Fast sorting algorithm with C language

Posted by paschim on Thu, 30 Apr 2020 14:20:07 +0200

Reference blog: http://blog.csdn.net/zhengqijun/article/details/53038831

1, Quicksort algorithm

1. Definition

Fast sorting was proposed by C. A. R. Hoare in 1962. Fast sorting is an improvement of bubble sorting, which adopts a divide and conquer strategy.


2. Basic ideas

The data to be sorted is divided into two independent parts by a single sorting. All the data in one part is smaller than all the data in the other part, and then the two parts are sorted rapidly according to this method. The whole sorting process can be carried out recursively, so that the whole data becomes an ordered sequence.

3. Steps

a. First, take a number from the sequence as the reference number.

b. In the process of partition, put all the numbers larger than this number to its right, and all the numbers smaller than or equal to it to its left.

c. Repeat the second step for the left and right intervals until there is only one number for each interval.

2, C language implementation code (for reference only)

  1. /***************************************************** 
  2. File name: Quicksort 
  3. Author: Zhengqijun    Version:1.0    Date: 2016/11/04 
  4. Description: Quick sorting of arrays 
  5. Funcion List: Implement fast sorting algorithm 
  6. *****************************************************/  
  7.   
  8. #include <stdio.h>  
  9. #include <stdlib.h>  
  10.   
  11. #define BUF_SIZE 10  
  12.   
  13. /************************************************** 
  14.  *Function name: display 
  15.  *Function: print array elements 
  16.  *Parameters: array - printed array, maxlen - number of array elements 
  17.  *Return value: None 
  18.  **************************************************/  
  19. void display(int array[], int maxlen)  
  20. {  
  21.     int i;  
  22.   
  23.     for(i = 0; i < maxlen; i++)  
  24.     {  
  25.         printf("%-3d", array[i]);  
  26.     }  
  27.     printf("\n");  
  28.   
  29.     return ;  
  30. }  
  31.   
  32. /******************************** 
  33.  *Function name: swap 
  34.  *Function: exchange the values of two numbers 
  35.  *Parameters: two numbers exchanged 
  36.  *Return value: None 
  37.  ********************************/  
  38. void swap(int *a, int *b)    
  39. {  
  40.     int temp;  
  41.   
  42.     temp = *a;  
  43.     *a = *b;  
  44.     *b = temp;  
  45.   
  46.     return ;  
  47. }  
  48.   
  49. /************************************ 
  50.  *Function name: quicksort 
  51.  *Function: fast sorting algorithm 
  52.  *Parameters: 
  53.  *Return value: None 
  54.  ************************************/  
  55. void quicksort(int array[], int maxlen, int begin, int end)  
  56. {  
  57.     int i, j;  
  58.   
  59.     if(begin < end)  
  60.     {  
  61.         i = begin + 1;  //Take array[begin] as the reference number, so compare with the reference number from array[begin+1]!  
  62.         j = end;        //array[end] is the last bit of the array  
  63.             
  64.         while(i < j)  
  65.         {  
  66.             if(array[i] > array[begin])  //If the array element being compared is greater than the base number, the position is swapped.  
  67.             {  
  68.                 swap(&array[i], &array[j]);  //Exchange two numbers  
  69.                 j--;  
  70.             }  
  71.             else  
  72.             {  
  73.                 i++;  //Move the array one bit backward to continue the comparison with the base number.  
  74.             }  
  75.         }  
  76.   
  77.         /* After jumping out of the while loop, i = j. 
  78.          * At this time, the array is divided into two parts -- > array[begin+1] ~ array[i-1] < array[begin] 
  79.          *                           -->  array[i+1] ~ array[end] > array[begin] 
  80.          * At this time, array is divided into two parts, and then array[i] is compared with array[begin] to determine the location of array[i]. 
  81.          * Finally, exchange array[i] and array[begin] to sort the two parts! And so on, until the last I = j does not meet the conditions to exit! 
  82.          */  
  83.   
  84.         if(array[i] >= array[begin])  //Here, you must wait "> =" or an error will occur when the array elements are of the same value!  
  85.         {  
  86.             i--;  
  87.         }  
  88.   
  89.         swap(&array[begin], &array[i]);  //Exchange array[i] and array[begin]  
  90.           
  91.         quicksort(array, maxlen, begin, i);  
  92.         quicksort(array, maxlen, j, end);  
  93.     }  
  94. }  
  95.   
  96. //Main function  
  97. int main()  
  98. {  
  99.     int n;  
  100.     int array[BUF_SIZE] = {12,85,25,16,34,23,49,95,17,61};  
  101.     int maxlen = BUF_SIZE;  
  102.       
  103.     printf("Array before sorting\n");  
  104.     display(array, maxlen);  
  105.       
  106.     quicksort(array, maxlen, 0, maxlen-1);  //Quick sort  
  107.       
  108.     printf("Sorted array\n");  
  109.     display(array, maxlen);  
  110.       
  111.     return 0;  
  112. }  


The results of the procedure are as follows:


The appeal code combines my own view and understanding of quick sorting for reference only.



Topics: C