One of the Java algorithm series: Fast sorting algorithm

Source: Internet
Author: User
Tags benchmark


1, algorithm concept.

Quick Sort (Quicksort) is an improvement to the bubbling sort. by C. A. R. Hoare was introduced in 1962.

2, the algorithm idea.

By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

3, realize the idea.

① with the first keyword K 1 as the control word, will [K 1, K 2,..., K N] into two sub-regions, so that all the keywords in the left area is less than or equal to K 1, all the keywords in the right is greater than or equal to K 1, and the last control of the place in the middle of two sub-regions. The data is still in an unordered state within the sub-region.
② left area as a whole, with ① steps to deal with, the right area for the same treatment. (i.e. recursion)
③ Repeat steps ①, ②, until the left area has been processed.


The time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.

Worst time complexity: The worst case is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-interval is divided, Only one less than the number of records in the unordered area before partitioning. Therefore, the fast sorting must do N-1 division, the first Division of the beginning of the interval length of n-i-1, the required number of comparisons is N-i (1<=i<=n-1), so the total number of comparisons reached the maximum Cmax =n (n-1)/2=o (n^2). If the partitioning algorithm given above, each time the current unordered area of the 1th record as the benchmark, then when the file's record has been in an ascending order (or descending order), each partition is taken by the baseline is the current unordered region of the minimum (or maximum) of the record, the fast sort will require the most number of comparisons.

The best time complexity: In the best case, each partition of the datum is the current unordered region of the "median" record, the result of the Division and the datum of the left and right two unordered sub-range of the length of approximately equal. The total number of keyword comparisons is O (NXLGN).

It is easier to use recursive trees to analyze the best case comparisons. Because the left and right sub-interval length is roughly equal after each division, so the height of the recursive tree is O (LGN), and the recursive tree each layer on each node corresponding to the division of the number of key words required in the process of the sum of the total number of keys not more than N, so the overall ordering process required to compare the overall frequency of C ( Because fast-sorting records move no more than the number of comparisons, the worst-case complexity for fast sorting should be O (n^2), and the best time complexity is O (NXLGN).

Selection of datum keywords: selecting the divided Datum keywords in the current unordered area is the key to determining the performance of the algorithm.

① the "Three take in" rule, that is, in the current interval, the first, the end of the interval and the middle position of the keyword comparison, the value of the three corresponding to the record as a benchmark, before the beginning of the division of the benchmark record and the 1th record of the zone Exchange, after the division process and the above given the Partition algorithm exactly the same

② the random number K (Low<=k<=high) between low and high, using r[k] as the benchmark; the best way to select a datum is to generate a random number K (Low<=k<=high) between low and high with a random function, Use R[k] as a benchmark, which is equivalent to forcing R[low. The records in high] are randomly distributed. The quick sort obtained by this method is generally called a random quick sort. The random quick sort is very different from the general fast sorting algorithm. However, after randomization, the performance of the algorithm is greatly improved, especially for the initial order of the file, it is generally not possible to cause the worst case occurrence. The randomization of the algorithm is not only suitable for fast sequencing, but also for other algorithms that require random distribution of data.

Average time complexity: Although the worst time for fast sorting is O (n^2), in terms of average performance, it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NXLGN).

Spatial complexity: Fast sorting requires a stack within the system to implement recursion. If each partition is more uniform, the height of its recursive tree is O (LGN), so the required stack space after recursion is O (LGN). In the worst case, the height of the recursive tree is O (n), and the required stack space is O (n).

Stability: Fast sequencing is non-stable.


Code:

public static void QuickSort (Int. sort[],int left,int right) {int L=left;int r=right;int pivot =sort[left];if (l <r) {Whil E (L<r) {while (l<r&&sort[r]>=pivot) {r--;} if (l<r) {int temp =sort[r];sort[r] = sort[l];sort[l]=temp;l++;} while (L<r && sort[l]<=pivot) {l++;} if (l<r) {int temp =sort[r];sort[r] = sort[l];sort[l]=temp;r--;}} Sortmethod.printsort (sort); QuickSort (SORT,LEFT,L-1); QuickSort (sort,l+1,right);}}


Implementation method:

public static void QuickSort (Int. sort[],int Left,int right) {int. dp;if (left <right) {DP =partition (sort,left,right); Sortmethod.printsort (sort); QuickSort (sort,left,dp-1); QuickSort (sort,dp+1,right);}} private static int partition (int[] sort, int left, int. right) {//TODO auto-generated method Stubint Pivot =sort[left];whi Le (left<right) {while (left<right&& Sort[right] >=pivot) {right--;} if (left<right) {sort[left++] =sort[right];} while (left <right && Sort[left] <=pivot) {left++;} if (left <right) {sort[right--] =sort[left];}} Sort[left] =pivot;return left;}

Sort Call Code:

public class Insertsort {public static void main (String arg[]) {int a[]={79,32,23,67,28,3,36,76,98,19}; Sortmethod.quicksort (A, 0, a.length-1); Sortmethod.printsort (a);}}

Sorting process:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

One of the Java algorithm series: Fast sorting algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.