C Speech rapid sorting algorithm and code

Source: Internet
Author: User

the Rapid sequencing is an improvement on the sort of bubbling method.
the fundamental thinking of the rapid sorting algorithm is: to stop the sorting of the number into your two parts, a part of all the data is smaller than the other part of the data, and then divide the two local data to stop the strange division, repeatedly perform the above division operations, until all the data to stop sorting into order.
can only be based on the fundamental thinking of the rapid sort of view is not deep, next to n unordered series a[0], a[1] ..., a[n-1] Take the rapid sorting method stop the ascending display as an example stop the Explanation.
(1) define two variables low and high, set low and high to the beginning of the sequence to stop sorting and the initial element of the Subscript. For the first time, the value of low and high is identified as 0 and n-1, and the next each value is determined by dividing the missing sequence element and the index of the first Element.
(2) Define a variable key, followed by the value of the key to divide the array A into your two local, usually, the key value is to stop the ordering sequence of the first element Value. The first value is a[0], and each subsequent value is determined by the element to divide the Sequence.
(3) from the beginning of the array element of high point to scan to the left, while scanning the array element labeled high to stop comparing with the partition Datum key, until high is not low or find the first array element less than the base value key, The value is then assigned to the array element pointed to by low, while the low is shifted to the right by one Position.
(4) If low is still less than high, then the array element pointed to by low is scanned to the right, and the array element value of low is scanned at the same time as the base value of the partition key stop comparison operation, until low is less than high or find the first array element greater than the base value key , and then assigns the value to the array element to which high is pointing, while moving the high to the left one Position.
(5) repeated steps (3) (4), until the low of the plant is not less than high, then the victory after the loss of the two local identified as a[low......pos-1] and a[pos+1......high], medium, The value of the array element corresponding to the POS subscript is the base value of the stop partition key, so the array element labeled POS is assigned a value of key when the partition is Complete.
(6) will divide the loss of you two local a[low......pos-1] and a[pos+1......high] continue to take the above steps to stop division, until the loss of ordered Sequence.
in order to deepen the Reader's understanding, following a code to understand the rapid sequencing of detailed Completion.

#include  <stdio.h>  #include  <stdlib.h>  #define  N 6 int  Partition (int arr[], int low, int high) { int key; key = arr[ Low]; while (low

Operational consequences:

Sort before 32 12 7 78 23 45 sorted 7 12 23 32 45 78

in the following code, according to the pace of the introduction of the step-up to complete the rapid sequencing algorithm. next, The representation diagram shows the first partitioning Operation.
in the first Division operation, the ancestor line is initially set, the value of key is the base of the stop division, whose value is the first element value to be divided into the array, the first element value 32 in the following sort sequence, and low set to the subscript to sort the first element in the array, with a value of 0 for the first sort Operation. Set high to the subscript for the first element of the sorted sequence, with the first value of 5 in the following sort Sequence. The array element labeled high first stops comparing with key, because the element value is greater than key, so high-left moves a status continuous Scan. Because the next value is 23, less than the value of key, 23 is assigned to the array element that is labeled LOW. Next move the low right one position, the value of the array element pointed to by the low is compared to the key stop, the next 12, 7 is less than key, so low continues to the right to scan, until the subscript low point to the array element value 78 is greater than the key, Assigns a value of 78 to the array element that is labeled high and moves the high to the left one Position. Because low is no longer less than high, the division is Complete. The need to keep in mind is that in the process of stopping dividing, it is mostly to stop comparing the value of the scan with the value of key, if it is less than key, then assign the value to one of the other elements in the array, and the value of the element is not Changed. As you can see, the requirement is assigned to the array element labeled POS at the beginning of the partition as the key value for the partition, and this element is no longer involved in the next partitioning Operation.

650) this.width=650; "src=" http://c.biancheng.net/cpp/uploads/allimg/140731/1-140I1201931J1.png "style=" border:0 px;width:470px;height:586px; "/>
First Division operation


After the first round is divided, you lose two local sequences a[0], a[1], a[2] and a[4], a[5], continue to divide, that is, each wheel divided after the loss of the two local sequence continued to divide until the order of the Lost Sequence.


This article is from the "11999725" blog, please be sure to keep this source http://12009725.blog.51cto.com/11999725/1843305

C Speech rapid sorting algorithm and code

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.