Fast sorting is a very useful sorting algorithm, although it is the worst case of Time complexity O (n^2), but the average time complexity is O (N*logn), and in memory usage, the complexity of the program algorithm is excellent, especially for the rapid sequencing of the possibility of randomization, Fast sorting is one of the most used algorithms.
Algorithm idea:
1. Create a temporary variable, save the rightmost value in the array, and form a pit on the far right.
2. Then find the value on the left that is greater than the temporary variable and put it in the pit so that the left side will form a new pit.
3. Look for the value on the right that is smaller than the temporary variable and put it in the new pit.
4. Know what to look for, and finally put the temporary variable in the last hole that was formed.
5. Divide this array into two subsequence features: the number in the left sub-sequence is less than or equal to the number in the right sub-sequence.
6. The subsequence is sorted by recursive invocation.
5. Know that the subsequence is complete and then merge. (Because the subsequence is an in-place sequence, the merge does not require an action, and the sort is completed)
Implementation code:
sort.h
Int singlesort (int *a, int left, int right) { assert (a); int tmp = a[right];//temporary variable Save value while (left < right) { //find the number on the left greater than or equal to TMP while (Left < right&&tmp >= a[left]) { left++; } if (left < right) { a[right--] = a[left];//the value to the right, then-- } //find the value on the right less than or equal to TMP while (Left < right&&tmp <= a [Right]) { right--; } if (left < right) { a[left++] = a[right];//the value to the left and then ++ } } a[left] = tmp; return left;} Void quicksort (int *arr, int left, int right) { if (Arr == null) { return; } if (left < right) { / /Recursive Call int div = singlesort (arr, left, right); quicksort (arr, left, div - 1); &Nbsp; quicksort (arr, div + 1, right); }}//print function Void print (int *arr, int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl;}
In Test.cpp
#include <iostream>using namespace std; #include "Sort.h" void Test () {int arr[] = {3, 9, 7, 6, 1, 2, 4, 8, 0, 5}; QuickSort (arr, 0,sizeof (arr)/sizeof (arr[0])-1); Print (arr, sizeof (arr)/sizeof (arr[0]));} int main () {Test (); System ("pause"); return 0;}
I have read a blog, a quick sort of a small range of numbers will take a lot of time, through my query data, the total number is less than 13, with a direct insert sort table better, more than this number with a quick sort. Then I changed the code a bit.
Direct Insert sort Void insertsort (int *a, int size) { int end; int tmp; for (int i = 0; i < size - 1; i++) { end = i; tmp = a[end + 1]; while (End >= 0 && tmp<a[end]) { a[end + 1] = a[end]; end--; &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[END&NBSP;+&NBSP;1] = tmp; }}void quicksort (int *arr, int left, int right) { if (arr == null) { return; } if (right < 13) { insertsort (arr, right + 1); } else { if (left < right) { //Recursive call int div = singlesort (arr, left, right); QuickSort (arr, left, div - 1); quicksort (Arr, div + 1, right); } }}
The above code is not good enough, if the quick sort is, your luck is poor, each take out the number is the biggest or the smallest, so he will become the worst case, the time complexity of O (n^2), in order to avoid this situation, I think of a way, three number to take the Chinese law,
What does it mean?
The three numbers are the leftmost, the rightmost, the middle of the three numbers, the number is not the largest or the smallest, so you can avoid the above situation.
Three-digit int mid (int *a, int left, int right) { int mid = left - (left - right); if (A[left] < a[right]) { if (A[left]>a[mid]) { return a[left]; } else if (A[right] < a[mid]) { return a[right]; } else { return a[mid]; } } else { if (A[right] > a[mid]) { return a[right]; } else if (A[left] < a[mid]) { return a[left]; } else { return a[mid]; } }}int Singlesort (int *a, int left, int right) { assert (a); int Tmp = mid (a,left,right);//temporary variable save value, three-digit while (left < right) { //find the number on the left greater than or equal to TMP while (left < right&&tmp >= a[left]) { left++; } if (left < right) { a[right--] = a[left];//the value to the right, then-- } //find the value on the right less than or equal to TMP while (left < Right&&tmp <= a[right]) { right--; } if (left < right) { a[left++] = a[right];//the value to the left. , and then ++ } } a [Left] = tmp; return left;}
This is quite perfect, if I don't want to use recursion, what should I do? Then I thought of the stack to do it.
Void quicksort (int *arr, int left, int right) { stack<int> s; s.push (left);//Subscript s.push (right);//stack array with subscript while (!s.empty ()) { Int tmpright = s.top (); s.pop (); int tmpleft = s.top (); s.pop (); //the number of the array into the left interval is less than or equal to the number of intervals int div = singlesort ( Arr, tmpleft, tmpright); Left and right of //pressure stackTwo-digit subscript if (tmpleft < DIV&NBSP;-&NBSP;1) { s.push (TmpLeft); s.push (div - 1); } //two-digit subscript in the right section of the press stack if (Div + 1 < tmpright) { s.push (div + 1); &nBsp; s.push (tmpRight); } }}
Writing here, I have thought of another way to implement the Singlesort () function, which feels more simple than the above, but is more abstract in understanding.
Thought:
1. Define a subscript for a[0] position cur
2. Define a previous position where the prev is a[0].
3. When the value of a[0] position is less than the rightmost value.
4.++prev, and then swaps the values corresponding to the prev and cur.
5. If it is less than the rightmost value, the ++prev,prev and rightmost values are exchanged.
6. Keep going until the left value is less than or equal to the value on the right.
Int singlesort (int *arr, int left, int right) { int cur = left; int prev = cur - 1; int key = mid (arr, left, right); swap (Arr[right] , key);//Put the value of key on the far right while (cur < right) { //++prev != cur prevent yourself from exchanging with yourself if (arr[cur]<=arr[right]&&++ Prev != cur) { swap (arr[prev], arr[ CUR]); } cur++; } swap (Arr[++prev], arr[right]); return prev;}
Analysis and implementation of fast sequencing