A quick sort of python sorting algorithm

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/AlwinXu/p/5424905.html

Quick Sort (quickSort)

The idea of a fast line: first select any data (usually the first number of the array) as the key data, and then put all the smaller than it in front of it, all the larger than its number is placed behind it, this process is called a quick sort.

Baidu Encyclopedia to give the algorithm:

A quick sorting algorithm is: 1) Set two variables I, J, the beginning of the order: I=0,J=N-1;2) with the first array element as the key data, assigned to key, i.e., key=a[0];3) from the start of J forward search, that is, after the start of the forward search (j--), Find the first value less than key A[j], will a[j] and A[i] interchange, 4) from I start backward search, that is, to start backward search (i++), to find the first greater than the key a[i], the a[i] and A[j] interchange, 5) repeat 3rd, 4 steps until i=j; (3,4 step, Did not find the qualifying value, that is, 3 a[j] is not less than the key,4 A[i] is not larger than the time to change the value of J, I, so that j=j-1,i=i+1 until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).

Complexity of Time: O (NLGN)

#QuickSort by AlvinDefQuickSort (mylist,start,end):#Determines whether low is less than high, and if false, returns directlyIf Start <End:i,j =Start,end#Set Base Number base =Mylist[i]While I <J:#If the number behind the list is larger or equal than the base number, move forward one up to a number smaller than the base numberwhile (I < j)and (Mylist[j] >=Base): j = j-1#If found, then the J element is assigned to the first element I, when the table i,j elements equal mylist[i] =MYLIST[J]#The same way comparing the first half areawhile (I < j)and (Mylist[i] <=Base): i = i + 1Mylist[j] = mylist[i] # after the first round of comparisons, the list is divided into two halves, and i=j, you need to set this number back to base mylist[i] = base # recursive front and rear half of the area QuickSort (MyList, start, i-1) QuickSort (myList, J + 1, end) return mylistmylist = [49,38,65,97,76,13,27 Print ("Quick Sort: ") QuickSort (Mylist,0,len (myList)-1)print (myList) 

A quick sort of python sorting algorithm

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.