A quick sort of introduction to algorithms

Source: Internet
Author: User
Tags shuffle

Quick Sortpersonal thoughts are chaotic, suggest to read the original text directlySimple version:
defPARTITION (A, P, R): x= A[r]#Anchor main element {greater than it put aside, less than put on the other side}i = P-1 forJinchRange (P, r):ifA[J] <=x:i+ = 1A[i], A[j]=A[j], A[i] a[i+1], a[r] = A[r], a[i+1]    returni + 1defQUICKSORT (A, P, r):ifP < r:#Divide and conquerQ =PARTITION (A, p, R) QUICKSORT (A, p, Q-1) QUICKSORT (A, Q+1, R)
View Code

Comment Version:
ImportRandomdefPARTITION (A, P, R): x= A[r]#Anchor main element {greater than it put aside, less than put on the other side}i = P-1 forJinchRange (P, r):ifA[J] <=x:i+ = 1A[i], A[j]=A[j], A[i] a[i+1], a[r] = A[r], a[i+1]    returni + 1"""partition Function Body Analysis: 1. The list, the first element subscript, the tail element subscript passed in 2. Set the base value to be the trailing element 3. Iterate through the list, comparing each item in the list to the base value 4. 1. If the current element is less than the base value, use the pointer to record subscript 2. If the current element is greater than the base value, over 3. If the current element is less than the base value, move the pointer back one bit, exchanging the current element with the element that the pointer now refers to, swapping position 5. The loop ends, the list is traversed, the elements on the left side of the current pointer are smaller than the datum values, the elements on the right are larger than the datum values, and the datum values are inserted into the position of this pointer""""""the parsing of two pointers inside the loop: 1. Set the tail element to the base value 2. The I pointer defaults to J-1, which is: -13. The J pointer varies according to the number of cycles, from 0 to the tail element subscript 4. 1. If each element is smaller than the baseline value from the beginning of the traversal, the I pointer simply records the position of the elements and moves backwards, and the position of the I-pointer passes is less than the base value of element 2. Then traverse backward, if the element that the J pointer points to is greater than the base value, skip 3 directly. Then traverse backwards, if the element that the J pointer points to is less than the base value, stop, the I pointer remains at the position of the element that is smaller than the base value, moves the I pointer back, I points to the first element that is larger than the base value, and the element is swapped with the element J points to position 5. 1. If the J pointer points to an element that is larger than the reference value, the value of I does not change by 2. If the J pointer points to an element that is smaller than the base value, the I pointer moves back one bit, and the I pointer points to the element that the J pointer points to, exchanging position 6. The loop ends, the list is traversed, the elements on the left side of the I pointer are smaller than the datum values, the elements on the right are larger than the datum values, and the datum values are inserted at the I pointer position"""defQUICKSORT (A, P, r):ifP < r:#Divide and conquerQ =PARTITION (A, p, R) QUICKSORT (A, p, Q-1) QUICKSORT (A, Q+1, R)"""QuickSort () function Body Resolution 1. The partition () function is split by the passed in list, 2. Split to the left list [both are smaller than the base value], the right list [is larger than the base value], recursively calls itself until it is divided into a small list of n list lengths of 3. To end the second step, Sort the list on behalf of"""if __name__=="__main__": A= [I forIinchRange (1, 1000)] Random.shuffle (A)Print("Shuffle the list after:"+str (A)) QUICKSORT (A, 0, Len (a)-1)    Print("list after the quick line:"+ str (A))
View Code

A quick sort of introduction to algorithms

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.