Quick Sort Comparison of C + + and Python

Source: Internet
Author: User


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

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.