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)
- /*****************************************************
- File name: Quicksort
- Author: Zhengqijun Version:1.0 Date: 2016/11/04
- Description: Quick sorting of arrays
- Funcion List: Implement fast sorting algorithm
- *****************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #define BUF_SIZE 10
- /**************************************************
- *Function name: display
- *Function: print array elements
- *Parameters: array - printed array, maxlen - number of array elements
- *Return value: None
- **************************************************/
- void display(int array[], int maxlen)
- {
- int i;
- for(i = 0; i < maxlen; i++)
- {
- printf("%-3d", array[i]);
- }
- printf("\n");
- return ;
- }
- /********************************
- *Function name: swap
- *Function: exchange the values of two numbers
- *Parameters: two numbers exchanged
- *Return value: None
- ********************************/
- void swap(int *a, int *b)
- {
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- return ;
- }
- /************************************
- *Function name: quicksort
- *Function: fast sorting algorithm
- *Parameters:
- *Return value: None
- ************************************/
- void quicksort(int array[], int maxlen, int begin, int end)
- {
- int i, j;
- if(begin < end)
- {
- i = begin + 1; //Take array[begin] as the reference number, so compare with the reference number from array[begin+1]!
- j = end; //array[end] is the last bit of the array
- while(i < j)
- {
- if(array[i] > array[begin]) //If the array element being compared is greater than the base number, the position is swapped.
- {
- swap(&array[i], &array[j]); //Exchange two numbers
- j--;
- }
- else
- {
- i++; //Move the array one bit backward to continue the comparison with the base number.
- }
- }
- /* After jumping out of the while loop, i = j.
- * At this time, the array is divided into two parts -- > array[begin+1] ~ array[i-1] < array[begin]
- * --> array[i+1] ~ array[end] > array[begin]
- * 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].
- * 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!
- */
- if(array[i] >= array[begin]) //Here, you must wait "> =" or an error will occur when the array elements are of the same value!
- {
- i--;
- }
- swap(&array[begin], &array[i]); //Exchange array[i] and array[begin]
- quicksort(array, maxlen, begin, i);
- quicksort(array, maxlen, j, end);
- }
- }
- //Main function
- int main()
- {
- int n;
- int array[BUF_SIZE] = {12,85,25,16,34,23,49,95,17,61};
- int maxlen = BUF_SIZE;
- printf("Array before sorting\n");
- display(array, maxlen);
- quicksort(array, maxlen, 0, maxlen-1); //Quick sort
- printf("Sorted array\n");
- display(array, maxlen);
- return 0;
- }
The appeal code combines my own view and understanding of quick sorting for reference only.