Classic sorting algorithm-quick sort
Principle, through a scan of the data to be sorted into separate two parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence
As an example,
such as unordered array [6 2 4 1 5 9]
a)remove the first item [6] First,
Use [6] to compare the rest of the items in turn,
If the [6] is smaller than [6],2 4 1 5 are smaller than [6], so all put to the front of [6]
If compared to [6] large put [6] behind, 9 than [6] large, put to [6] behind,//6 out after a shout, than my small station front, bigger than my station behind, action!
After a trip, it becomes the following:
Sort before 6 2 4 1 5 9
After sort 2 4 1 5 6 9
b)proceed to the Ban La [2 4 1 5] for quick sequencing
Repeat step a) to change to the following:
Sort before 2 4 1 5
After sort 1 2 4 5
Before the ban La Sort is complete, the total sort is done as well:
Before sorting: [6 2 4 1 5 9]
After sorting: [1 2 4 5 6 9]
Sort End
The following code implementation is for reference only
Static intPartitionint[] unsorted,intLowintHigh ) { intPivot =Unsorted[low]; while(Low <High ) { while(Low < High && Unsorted[high] > Pivot) high--; Unsorted[low]=Unsorted[high]; while(Low < High && Unsorted[low] <= pivot) low++; Unsorted[high]=Unsorted[low]; } Unsorted[low]=pivot; returnLow ; } Static voidQuick_sort (int[] unsorted,intLowintHigh ) { intLOC =0; if(Low <High ) {Loc=partition (unsorted, Low, high); Quick_sort (unsorted, Low, loc-1); Quick_sort (unsorted, loc+1, high); } } Static voidMain (string[] args) { int[] x = {6,2,4,1,5,9 }; Quick_sort (x,0, X.length-1); foreach(varIteminchx) {Console.WriteLine (item+","); } console.readline (); }
Classic sorting algorithm-quick sort