Quick Sort Python

Source: Internet
Author: User
Tags benchmark

When it comes to quick sorting, it really takes a lot of effort to see and understand. The sorting algorithm is quite complex, and understanding it is like peeling onions, one layer at a level. OK, let's talk about the principle of the fast-line.

Fast sorting, like many online articles, is a divide-and-conquer, recursive way of constantly dividing and dividing. Each recursion will find the current benchmark value will be greater than this value of all the number to the right of the benchmark value, small to the left, and then the benchmark value to the left and the right of the two sub-array to do the same function.

The most central part of the fast track is finding the position of each benchmark value. Here are a few steps:

1. Pick a benchmark value key, generally select the first value of the array

2. A For loop, which iterates through the array starting with the second number, and each number is compared to this key

3. The purpose of the traversal is to find the appropriate position for the benchmark value key (i.e. the small one is on the left and the large on the right), by finding the number of m that is smaller than the benchmark value and, of course, moving the large value to the back.

4. If the array a[i] < key,m++, if M is not equal to the current position I, then a[i] This value is larger than key, need to move to the back;

5. The most difficult thing to understand in the fast-line is the shift exchange, which is the partition part. The value of I during traversal is incremented from 1 to end, while M holds the position of the benchmark value a[m].

Suppose A[i] has been smaller than key, then M and I will be the same, because to the last step, A[m] will and a[0] swap position, because the value of a[m] is smaller than key, so after the position, the benchmark value placed in the position, and the value of a[m] is smaller than key, It's right to put the first one.

When the value of A[i] is larger than key, M can no longer increase by 1 and I, because the position is to be swapped. If the next round is still a[i] larger than key, then M continues unchanged (the M position is always the critical position, followed by the key); until there is a number smaller than key, m increases by 1, and the number is ' rescued ' back. Exchange A[m] and A[i] values, place a value larger than key to the back, and move the value smaller than key to the position of a[m]. And so on.

The essence of the process of traversing an array of partition is that this m, which is able to get the position of the key by counting, can also "rescue" the number smaller than key from the back of the number larger than key, thus separating the ' size value '.


6. When the move is done, the value larger than the key is moved to the back, the value is smaller than the key is moved to the front, when the position of M is the critical point, followed by the key is larger than the keys, and then exchange the key value of a[0] and a[m], the completion of this time partition.

7. The next step is recursion. Consider the input array, which is smaller than key and larger than the key value, into the two same functions, and return to step 1, respectively.


There are many dynamic graphs on the Web:

650) this.width=650; "Src=" http://jbcdn2.b0.upaiyun.com/2012/01/ Visual-and-intuitive-feel-of-7-common-sorting-algorithms.gif "alt=" visual-and-intuitive-feel-of-7-common-so "/ >


Next is the Python implementation:

#!/usr/bin/pythona = [5,1,4,9,8]def quick_sort (Us_list, low, high):         if  (Low < high):         mid = _partition (Us_list, low, high)          Quick_sort (us_list, low, mid-1)         quick_sort (Us_list,  mid+1, high) def _partition (Us_list, low, high):         mid = low     mid_value = us_list[low]     for i in range (low+1, high+1):             if  (A[i] < mid_value):             mid += 1             if mid ! = i:                t  = us_list[mid]                 us_list[mid] = a[i]                 a[i] = t         print  a[mid],a[i]        print a    t =  a[low]    a[low] = a[mid]    a[mid] = t      return mid def main ():     quick_sort (a,  0, len (a)  - 1)     print aif __name__ ==  ' __main__ ':     main ()


Quick Sort Python

Related Article

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.