The idea of fast sorting algorithm
The fast sorting algorithm essentially divides an array into two sub-arrays, and then recursively calls itself for each sub-array for quick ordering, which applies the partitioning algorithm and recursive ideas, the specific partitioning algorithm reference http://www.cnblogs.com/iwebkit/p/ 7623350.html, as long as the division algorithm to understand the basic fast sort also understand.
Steps for fast sorting algorithms
1. Divide the array into left and right sub-arrays.
2. Call itself to sort the left array.
3. Call itself to sort the right array.
Hub selection of fast sorting algorithm
In the partitioning algorithm I wrote earlier, our hubs are defined by ourselves, but in a quick sort we need to use one of the items in the array as a pivot, and for the sake of understanding, we will use the right-most-left data item of the right array as a hub, and after the partition is complete, the data item in the leftmost end of the hub and right
Efficiency of fast algorithms
O (N*LOGN)
Fast-sorting Java programs
Comments in the program I wrote only the difference between the partitioning algorithm in the quick sort and the separate partitioning algorithm, if you want to understand the sorting sort see http://www.cnblogs.com/iwebkit/p/7623350.html, the Red code in the program is different. .
PackageSy;classarrayins{Private Long[] thearray; Private intNelems; PublicArrayins (intmax) {TheArray=New Long[Max]; Nelems= 0; } Public voidInsertLongvalue) {Thearray[nelems]=value; Nelems++; } Public voiddisplay () {System.out.print ("A ="); for(intj = 0; J < Nelems; J + +) {System.out.print (Thearray[j]+ " "); } System.out.print (""); } //Quick Sort, because we don't want to write parameters when we call, so we put the real quick sort in the method. Public voidQuickSort () {Recquicksort (0,nelems-1); } //Real Quick Sort Public voidRecquicksort (intLeftintRight ) { //Quick sort ends when you need to divide an array with only one data item if(Right-left < 1) { return; } Else { //defines the last data item of the current array as a hub LongPivot =Thearray[right]; //Call the partitioning algorithm, return the index value of the hub and assign it to Pivotindex intPivotindex =Partitionit (Left,right,pivot); //recursive fast algorithm, this recursive is the left decimal group of the current arrayRecquicksort (left,pivotindex-1); //recursive fast algorithm, this recursive is the right decimal group of the current arrayRecquicksort (Pivotindex + 1, right); } } //the partitioning algorithm in fast sorting Public intPartitionit (intLeftintRightLongpivot) { //define the left pointer, because the inner while thearray[++leftptr] is first self-increment, so here first minus one intLeftptr = left-1; //Define right pointer//here, in the partitioning algorithm, because the inner layer while the thearray[++leftptr] first self-subtraction, here first plus a right + 1, but in the fast algorithm, we will be the last array is the index value of right of the data items as a hub, So we're taking right-1, so we're not adding one here. int rightptr = right; while(true) { while(Leftptr < right && thearray[++ Leftptr] <pivot) { } while(Rightptr > Left && thearray[--rightptr] >pivot) { } //if the left and right pointers meet, the partitioning algorithm ends if(Leftptr >=rightptr) { Break; } Else{swap (LEFTPTR,RIGHTPTR); } } //The last item in the array, which is the data item with the index value right, is the pivot, where we place the pivot where he should be in the middle of the two decimal groups, the position of the left pointer.swap (RIGHT,LEFTPTR); returnleftptr; } //Exchange Method Public voidSwapintIndex1,intindex2) { Longtemp =THEARRAY[INDEX1]; THEARRAY[INDEX1]=Thearray[index2]; THEARRAY[INDEX2]=temp; }}classapp{ Public Static voidMain (string[] args) {intMaxSize = 16; Arrayins arr; Arr=NewArrayins (maxSize); for(intj = 0; J < MaxSize; J + +) { Longn = (int) (Java.lang.Math.random () *199); Arr.insert (n); } arr.display (); Arr.quicksort (); Arr.display (); }}
Fast sorting algorithm