Fast sorting is a relatively high efficiency in the sorting algorithm, but the use of people is relatively small, everyone generally handy sorting algorithm is the bubble sort. Because bubble sort is subjective, easy to understand, and quick sort uses recursion, you may be a little overwhelmed.
Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data will be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data to be quickly sorted, the entire sorting process can be recursive To achieve an orderly sequence of the entire data.
I looked down on the Internet some Bolg write sort algorithm, some understand wrong, some is too complex, and there is simply a temporary array, rather than in-place sorting. Of course, my also not much good, just enough to put a train of thought;
tell me the basic idea: take the first element of the array each time as a The standard of comparison (sentinel Element), which is larger than the Sentinel element, is placed on its right side, and the one that is smaller than this sentinel element is placed on its left side;
The approximate steps:
1, judging the parameter conditions, in fact, this is the export of recursion;
2, the first element of the array is the sentinel element, let the other elements and it compare size; (remember that the first element is at the mouth, because the value inside is saved as a sentinel element)
3. Start by looping forward from the end of the array to an element a that is less than the sentinel element, place the element A at the first element position (that is, the sentinel element position, because the Sentinel element position is empty); (remember that the position of element A is empty)
4. Begin to loop back from the head of the array to get an element B greater than the Sentinel element, and place the element B in the position of the element a removed from the previous step;
5, loop the above 3, 4 steps, until the last element, then the last element will hold the Sentinel element.
6, the part that is smaller than the Sentinel element and the part that is larger than the Sentinel element are called recursively, recursively ordering all the elements in turn;
Code section:
#include <stdio.h>//print array void print_array (int *array, int length) {int index = 0; printf ("array:\n"); for (; index < length; index++) {printf ("%d,", * (Array+index)); } printf ("\ n"); } void QuickSort (int array[], int length) {int start = 0; int end = Length-1; int value = array[start];//Gets the Sentinel element if (1 > length) return;//recursive exit while (Start < end) {//with Sentinel element as standard, divided into greater than it and less than it The two-column element while (Start < end) {//loops forward from the array to get a element smaller than the Sentinel element if (array[end--] < value) { array[start++] = Array[++end]; Break }} while (Start < end) {///from the head of the array to iterate over an element greater than the Sentinel element if (array[start++] > value) { array[end--] = Array[--start]; Break }}} Array[start] = value;//Place Sentinel element printf ("\nstart:%d, end:%d\n", start, end);//This is the test under STA RT and End are the same quickSort (array, start);//Recursive sortThe column element that is smaller than the Sentinel element is QuickSort (array + start + 1, length-start-1);//The column with a recursive sort greater than the sentinel element} int main (void) {int array[12] = {1,11,12,4,2,6,9,0,3,7,8,2}; Print_array (array, 12);//Start printing the next QuickSort (array, 12);//Quick Sort Print_array (array, 12);//Sort and print down return 0; }
Operation Result:
Reprint please indicate the author and source, original address: http://blog.csdn.net/yuzhihui_no1/article/details/44198701
If there is not the right place, I hope you correct, learn together! Thank you!!!
A quick sort of sorting algorithm