The basic idea of C + + quick Sort is to take out one bit as the contrast bit X, to start probing from both ends of the sequence, to find a number larger than x from the right to the left, to find a number smaller than x from the left to the right, and then to exchange them and loop to i=j. This time after the swap is over, X is swapped to the middle, because the left is smaller than it and the right side is bigger than it, so it's in the middle. At the end of the function, there is a recursive function, respectively, on the left and right side of the order, until the element is decomposed into 1 stops, the loop ends.
void quicksort (int left,int right) {int i,j,t,temp;if (left>right) return;temp = A[left];i = Left;j = Right;while (i!=j) { while (a[j]>=temp && i<j) j--;while (a[i]<=temp && i<j) i++;if (i<j) {t = a[i];a[i] = a[j];a[ j] = t;}} A[left] = A[i];a[i] = Temp;quicksort (left,i-1); quicksort (i+1,right);}
The quick sort implementation in Python is similar, but the code is a little bit simpler, creating two list elements at the beginning, extracting an element x from the queue, iterating through the entire queue, placing the element smaller than the x taken in less, and placing the element greater than x in the greater. Finally, a recursive expression is returned.
def quicksort (array): less = []greater = []if len (array] <= 1:return Arraypivot = Array.pop () for x in Array:if x <= p Ivot:less.append (x) else:greater.append (x) return quicksort (less) + [pivot] + quicksort (greater)
Quick Sort Comparison of C + + and Python