C language Fast Sort instance Code _c language

Source: Internet
Author: User

Fast sorting is an improvement to bubble sorting.

The basic idea of the fast sorting algorithm is: Divide the number of items to be sorted into the left and right two parts, where all of the data is smaller than the other part of the data, and then the two parts of the data are divided equally, repeating the partitioning until all the data to be sorted becomes ordered.

The understanding of the fast sort may be based only on basic ideas, followed by the a[0 of N unordered series], a[1] ..., a[n-1] using a quick sort method to explain the ascending arrangement.

(1) Define two variables low and high, and set low and high to the start element of the sequence to be sorted and the subscript of the last element. For the first time, the values for low and high are respectively 0 and n-1, and the next each value is determined by dividing the resulting sequence starting element and the subscript of the last element.

(2) Define a variable key, then divide the array a by the value of key to the left and right two parts, usually the key value is the first element value to sort the sequence. The first value is a[0], and each subsequent value is determined by the starting element to divide the sequence.

(3) The array element from high is scanned to the left, while scanning, the array elements marked high are then compared to the dividing Datum key, until the high is less than low or the first array element less than the Datum key is found, and then the value is assigned to the array element to which low is pointing , and move low to the right one position.

(4) If low is still less than high, the array element that is pointed to by low is then scanned to the right, while the value of the array elements labeled low is then compared to the divided Datum key, until low is less than high or the first array element larger than the Datum key is found. The value is then assigned to the array element to which the high is pointed, while the high is moved to the left one position.

(5) Repeat Steps (3) (4), until the low plant is not less than high, then after the successful division of the left and right two parts are a[low......pos-1] and A[pos+1......high], of which, POS subscript corresponding to the value of the array element is the base value of the partition key, So at the end of the partition, you also assign the value of the array element labeled POS to key.

(6) The left and right two parts of a[low......pos-1] and A[pos+1......high] continue to be divided by the above operational steps until an ordered sequence is obtained.

In order to deepen the reader's understanding, the following code is used to understand the specific implementation of the rapid sequencing.

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

Run Result:

Before sorting

32 12 7 78 23 45

After sorting

7 12 23 32 45 78

In the code above, the quick Sort algorithm is implemented step-by-step according to the steps described earlier. Next, the first partition operation is demonstrated by a schematic diagram.

In the first partition operation, the initial setting is first, and the value of the key is the benchmark for dividing, the value is the first element value to divide the array, the first element value 32 in the sort sequence above, and the low set to the subscript for the first element in the array, with a value of 0 for the first sort operation. Set high to the subscript for the last element of the sort sequence, and the first value in the sort sequence above is 5. The array element with the subscript high is compared to the key, and because the value of the element is greater than the key, the high is moved one position to the left to continue the scan. Because the next value is 23, less than the value of the key, the 23 is assigned to the array element to which the subscript low is pointing. Next, move low to the right one position, comparing the value of the array element of low to the key, which is less than key from the next 12 or 7, so low continues to move the scan right until the value of the array element that subscript low points to is 78, which is greater than key. Assigns 78 to the array element to which the subscript high is pointed, and moves high to the left one position. Next because the low is no longer less than high, the division ends. It should be noted that in the process of partitioning, the value of the scan is compared to the value of the key, if less than key, then the value is assigned to another element in the array, and the value of the element does not change. As can be seen from the diagram, it is necessary to assign the key value as the dividing datum at the end of the partition to the array element labeled POS, which no longer participates in the next partitioning operation.

First Division operation

After the first round division, we get the left and right two parts sequence a[0], a[1], a[2] and a[4], a[5, continue to divide, that is, the two-part sequence obtained after each wheel division continues to divide until the ordered sequence is obtained.

The above is a quick sort of C language in the details, hoping to help learn C language students grasp the fast sorting algorithm.

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.