Sort algorithms for algorithm learning: quick sorting

Source: Internet
Author: User

Quick sorting: quick sorting is an improvement in Bubble sorting. The basic idea is to split the records to be sorted into two separate parts by one sort. the keywords of some records are smaller than those of other records, then, the two records can be sorted separately to achieve the whole sequence order.


Specific Practices for quick sorting:

1. Two pointers low and high are attached. Their initial values are low and high, respectively, and the keyword of the pivot record is pivotkey.

2. First, search forward from the position indicated by high to find records with the first keyword less than tkey and pivot records exchange with each other.

3. Search backward from the position indicated by low and find records with the first keyword greater than the pivotkey and pivot records exchange with each other.

4. Repeat steps 2 and 3 until low = high.

The preceding steps are illustrated as an example:

Initial Keyword: 49 38 65 97 76 13 27 49

1. the keyword with low and high and pivot record is pivotkey:

49 38 65 97 76 13 27 49

Lower (low) lower (high)

Keys (unique tkey)

2. search forward from the position indicated by high and find the first record smaller than tkey (27 here) and the pivot record exchange with each other:

27 38 65 97 76 13 49

Lower (low) lower (high)

Keys (unique tkey)

3. Search backward from the position indicated by low and find the first record greater than tkey (65 here) and the pivot record exchange with each other:

27 38 49 97 76 13 65 49

Lower (low) lower (high)

Keys (unique tkey)

4. Repeat steps 2 and 3 until low = high.

27 38 13 97 76 49 65 49

Lower (low) lower (high)

Keys (unique tkey)

27 38 13 49 76 97 65 49

Lower (low) lower (high)

Keys (unique tkey)

27 38 13 49 76 97 65 49

Lower (Low = high)

Keys (unique tkey)

The above shows a quick sorting process. The entire quick sorting process can be recursive. If there is only one record in the column to be sorted, it is obviously ordered. Otherwise, the two subsequences obtained by the split are sorted in a quick order.


Sample Code 1 (in C ):

/*************************************** * ***************************** Author: li Bing Date: 2014-9-11 Email: [email protected] @ array: the pointer to the records @ low high ******************************** *************************************/// data exchange void swap (int * num1, int * num2) {int TMP = * num1; * num1 = * num2; * num2 = TMP;} // int partition (INT array [], int low, int high) {int lower tkey = array [low]; while (low 

In the previous quick sorting process, the value assignment operation of the three records is used, that is, the three values in the SWAp function, but the value assignment to the pivot record is redundant, this is because the position when low = high is the last position of the pivot record only when a fast sorting is completed. Therefore, the rewrite algorithm is as follows:

Sample Code 2 (in C ):

// A quick sorting of int partition (INT array [], int low, int high) {int partition tkey = array [low]; while (low 

Note:

1. In terms of average time, quick sorting is currently considered as the best internal sorting method.

2. Fast sorting is considered to be the best average performance among all sorting methods of the same order of magnitude (O (nlogn.

3. When selecting the pivot value, you can select the pivot record according to the rule of "getting the three", that is, compare the one in the middle of the low, high, and mid values as the pivot value, this greatly improves the performance of quick sorting in the worst case.


Summary:

1. In terms of time, the average performance of quick sorting is better than that of other sorting methods.

2. In terms of space, quick sorting requires a stack space to implement recursion.


References:

1. Edited by Yan Weimin Wu Weidong, data structure (C language version)

2. http://blog.csdn.net/morewindows/article/details/6668714

3. http://blog.csdn.net/to_be_it_1/article/details/37866391


Sort algorithms for algorithm learning: quick sorting

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.