Sort (5) --------- fast sort (implemented in C)

Source: Internet
Author: User

After shell invented shell sorting, everyone in the computer field began to get upset again. Why can't he invent it. So another buddy came out. Ah... So many sorting methods are invented by the Chinese. By the way, the history books of the tianchao dynasty in one hundred years are still being blown up. The first time zu chongzhi discovered the circumference rate, he has been ahead of the world for more than one thousand years ......


Quick sorting uses the Divide and conquer policy to Divide a serial (list) into two sub-serial (sub-lists ).
Steps:

1. Pick out an element from the series, which is called a "benchmark ",
2. Re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After the partition exits, the benchmark is in the middle of the series. This is called a partition operation.
3. recursively sort the subseries smaller than the reference value element and the subseries greater than the reference value element.
4. The bottom of recursion is that the sequence size is zero or one, that is, it is always sorted. Although this algorithm is always recursive, it always exits, because in each iteration, it will at least place an element at its final position.


① First, assume that the first element of the sequence is a flag, which is always compared with the flag from the first to the last. All elements smaller than the flag are moved to the left of the flag, move all greater than the flag to the right of the flag.

② Perform step ① on the left side of the flag, and form two new signs on the right side.


Source code:

# Include "stdafx. h "# include <stdlib. h> void swap (int arr [], int I, int j) // exchange {int temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} int getPoint (int arr [], int low, int high) {int res = arr [low]; // for the first time, assume that the first element is the flag point while (low 

Running result:

Before sort: 8 20 31 1 29 16 27 21 1 11 After sort: 1 1 8 11 16 20 21 27 29 31 Please press any key to continue...


In case of any errors, please do not hesitate to point out.

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.