Quick sort sorting algorithm of ten classic

Quick sort sorting algorithm of ten classic

These days suddenly want to look back at the top ten classical sorting algorithms, so they find free time to realize what c language, when it is reviewed and reviewed, today we have to say something about fast sorting algorithm

      Ten classic fast sorting algorithm sorting algorithm is one of its main idea is to not order a set of numbers, in this first set of figures where to go first to a number as a reference number, generally it will be the first to use this set of numbers The last number or a number as a reference number. Here we choose the first data set of this number as a reference number. Then set the two cursors, we will point to the first number before the cursor, then we point to the cursor after the last number of the set of data. Then left from the cursor when found smaller than the reference digital numbers, we will be the number of pre-cursor and point to the digital switching, then the cursor before the increment (plus one, or a pre-cursor to move backward) . Then we began the previous cursor right to find, when finding large numbers than the reference number, we will exchange the figure number and the cursor is currently pointing. Then the cursor from minus (minus one, after or move the cursor forward one). Then when the above steps are repeated until the cursor is equal to the previous cursor, the end of the sort.
      However, it should be noted that the above sorting process, only a small number of reference than the benchmark figure on the left is greater than the baseline figures, on the basis digits to the right, so in order to complete this set of data sorting is completed . We also need to further sort, in fact, we will be on top of the array is divided into two parts, one smaller than the benchmark figure, a part is greater than the reference numbers, so we just need to be followed again to sort these two parts on the line, the left part is divided into two parts again, until finally can not continue to be divided, then the sort is complete. Having said that, some students still may not understand, we draw a simple diagram to assist us to understand, as follows:

1  / * 
2     classical scheduling algorithm: c language quick sort algorithm to achieve
 . 3  * /  
. 4 #include <stdio.h>
 . 5 #include <stdlib.h>
 . 6  // function display array elements 
. 7  void the display ( int A [] , int len) {
 . 8      int I = 0 ;
 . 9      the while (I < len) {
 10          the printf ( " % D " , A [I ++ ]); 
 . 11      }
 12 is      the printf ( " \ n- " );
 13 is  }
 14  
15 // swap function of two numbers 
16  void the swap ( int * First, int * SECOND) {
 . 17      int TEMP;
 18 is      TEMP = * First;
 . 19      * * = First SECOND;
 20 is      * SECOND = TEMP;
 21 is  }
 22 is  
23 is  // the array element divided into two parts, larger than the reference element and the reference element is less than 
24  int Sort ( int a [], int Start, int End) {
 25       int Standard = a [Start];
 26 is       int Low = Start ;
 27      int high = end;
28      while(low<high){
29          while(low<high && a[high]>=standard){
30              high--;
31          }
32          
33          if(low<high){
34              swap(&a[low],&a[high]);
35              low++;
36          }
37          
38          while(low<high && a[low]<=standard){
39              low++;
40          }
41          
42          if(low<high){
43              swap(&a[low],&a[high]);
44              high--;
45          }
46      }
47     a[low] = standard; 
48     return low; 
49 }
50 
51 //快速排序函数
52 void quickSort(int a[],int start,int end){
53     if(start<end){
54         int pos = sort(a,start,end);
55         quickSort(a,start,pos-1);// recursive call 
56 is          QUICKSORT (A, + POS . 1 , End); // recursive call 
57 is      }
 58  }
 59  int main ( void ) {
 60      int len = 0 ;
 61 is      int * P;
 62 is      int I;
 63 is      the printf ( " Please sorted input array length: \ n- " );
 64      Scanf ( " % D " , & len);
 65      getchar (); // read walking carriage return character input stream 
66      P = ( int*) The malloc ( the sizeof ( int ) * len);
 67      the printf ( " Enter a number ordered: \ n- " );
 68      for (I = 0 ; I <len; I ++ ) {
 69          Scanf ( " % D " , & P [I]);
 70      }
 71 is      the printf ( " pre-sorted array: \ n- " );
 72      the display (P, len);
 73 is      QUICKSORT (P, 0 , len . 1 ;)
 74      the printf ( " sorted array : \ the n- ");
 75      the display (P, len);
 76      Free (P); // release the memory 
77      return  0 ;
 78 }

 

 

      Simple introduction above code, the display function formal parameter is a two array of integers, a is the length of the array, the function of this function is to output the elements of the array to the console. exchange swap function is a function of the value of two numbers. Then sort function is the array element divided into two parts, one part is smaller than the reference part of the element, the other part is larger than the reference element portion. In quickSort function, we call the sort function to obtain the index we divide good reference element is located, then the index for the sector, the front part of the data and then sort, part of the data back again to sort until the end.
      Results are given below code runs:

Guess you like

Origin www.cnblogs.com/ybxfighting/p/11671449.html