Algorithm sorting algorithm--Quick sort

Source: Internet
Author: User

Quick sorting is an improvement on the sort of bubbling method.

The basic idea of a fast sorting algorithm is to divide the number of orders that are 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 divide the two parts of the data into the same division, repeating the above partitioning operations until all the data to be sorted becomes orderly.

May be based only on the basic idea of the rapid sort of understanding is not deep, next to N unordered series A[0], a[1] ..., a[n-1] Use the Quick Sort method for ascending arrangement as an example to explain.

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

(2) Define a variable Val, typically, the Val value is the first element value to be ordered in the sequence. The value for the first time is a[0].


(3) The array element pointed to by high starts scanning to the left, and the array element labeled high is compared to Val at the same time, until high is not high or the first array element less than Val is found, and the value is assigned to the array element pointed to by low. Then you don't move the high.

(4) If low is still less than high, then the array element pointed by low starts scanning to the right, and the array element values labeled low are compared to Val at the same time, until low is less than high or the first array element greater than Val is found. The value is then assigned to the array element to which high is pointing.

(5) Repeat Steps (3) (4) until Low=high, then the successful division of the left and right two parts are a[low......pos-1] and A[pos+1......high], wherein the POS subscript corresponds to the value of the array element is Val, So at the end of the partition, the array element that is labeled POS is also assigned a value of Val. PS: After a round of comparisons, just to find the location of Val, the other array elements are still unordered.

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

To be able to deepen the reader's understanding, a piece of code is then used to understand how the quick sort is implemented.

1#include <stdio.h>2#include <stdlib.h>3 #defineN 64 intFindpos (intArr[],intLowintHigh ) {5     intVal;6val =Arr[low];7      while(low<High ) {8          while(Low val)9high--;TenArr[low] =Arr[high]; One  A          while(Lowval) -low++; -Arr[high] =Arr[low]; the     } -Arr[low] =Val; -     returnLow ; - } + voidQuick_sort (intArr[],intLowintHigh ) { -     intPos; +     if(low<High ) { Apos =Findpos (arr, low, high); atQuick_sort (arr,low,pos-1); -Quick_sort (arr,pos+1, high); -     } -     return; - } - intMainvoid){ in     inti; -     intarr[n]={ +, A,7, +, at, $}; toprintf"sort before \ n"); +      for(i=0; i<n;i++) -printf"%d\t", Arr[i]); theQuick_sort (arr,0, N-1); *printf"\ n sort after \ n"); $      for(i=0; i<n; i++)Panax Notoginsengprintf"%d\t", Arr[i]); -printf ("\ n"); theSystem"Pause"); +     return 0; A}

Operation Result:

Sorted before    7    45 after sorting 7    78

In the above code, the fast sorting algorithm is implemented in step step, according to the steps described earlier. The next step is to demonstrate the quick sort.

1> Start is:

2> because high is pointing to 45:32 large, so it moves to the left

3> because the high point of 23:32 small, so the 23 is assigned to low

4> because low points to 23:32 small, so the right shift

5> because low points to 12:32 small, so the right shift

6> because low points to 7:32 small, so the right shift

7> because the low point of 78:32, so the 78 is assigned to high

8> because high points to 78:32, so move to the left

9> so low and high coincide, the position of low is the final position of Val

10> then low to the array elements in the Pos-1 range, and then sort by the above operation

11> then pos+1 to the array elements in the high range, and then sort by the above operation

Algorithm sorting algorithm--Quick sort

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.