---fast sorting of classical sorting algorithms

Source: Internet
Author: User

A quick sort is an optimization of the bubbling sort, with the idea of dividing it into two halves, each time sorting

Suppose the user enters an array like this:
Subscript 0 1 2 3 4 5
Data 6 2 7 3 8 9
Create a variable i=0 (point to the first data), j=5 (point to the last data), k=6 (assigns the value of the first data). We're going to move all the numbers smaller than K to the left of K, so we can start looking for a smaller number than 6, starting with J, from right to left, constantly diminishing the value of the variable J, we find the first subscript 3 of the data is smaller than 6, so the data 3 is moved to subscript 0 position, the subscript 0 of the data 6 to subscript 3, the first comparison is completed:
Subscript 0 1 2 3 4 5
Data 3 2 7 6 8 9
I=0 j=3 K=6 Then, to start the second comparison, this time to become a bigger than the K, and to go after the search. Sliding scale variable I, found that the data of subscript 2 is the first larger than the K, so with the subscript 2 of the data 7 and J points to the subscript 3 of the data of 6 to exchange, the data state into the following table:
Subscript 0 1 2 3 4 5
Data 3 2 6 7 8 9
i=2 j=3 K=6 says the above two comparisons are a cycle. Then, the variable J is decremented again, repeating the above cycle comparison. In this example, we do a loop and find I and J "Meet": they all point to subscript 2. So, the first time the comparison is over. The results are as follows, usually K (=6) the number on the left is smaller than it, the number of K right is larger than it:
Subscript 0 1 2 3 4 5
Data 3 2 6 7 8 9
If I and J do not meet, then sliding scale I find large, not yet, and then diminishing J to find small, so repeated, continuous circulation.     Attention to judgment and search is carried out at the same time.     Then, the data on both sides of the K, and then the process is divided into the above, until no further grouping. Note: The first time quick sort does not directly get the final result, only the number smaller than K and K is divided into the two sides of K. To get the final result, you need to perform this step again on an array of subscript 22 edges, and then decompose the array until the array is no longer decomposed (only one data) to get the correct result. The code is as follows:
void QuickSort (struct sq_list *v, int left, int. right) {if (left < right) {int key = V->elem[left].id;int Low = Left ; int high = Right;while (Low < High) {when (Low < high && v->elem[high].id > Key) high--;if (Low < High) {v->elem[low].id = V->elem[high].id;++low;} while (Low < high && v->elem[low].id < key) Low++;if (Low < high) V->elem[high].id = V->elem[low]. Id;--high;} V->elem[low].id = key; QuickSort (V, left, low-1); QuickSort (V, low + 1, right);}}

  

---fast sorting of classical sorting 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.