Quick Sort
First find a number as the Datum point, the number is greater than the datum point to the right, the number less than or equal to the datum point on the left, and then use recursion, on the left and right side of the datum point to do the same operation, when the number of recursive entry only one can stop recursion.
How do I put the number that is less than or equal to the datum point to the left and the number above the datum point to the right?
For convenience, select the first number of the array as a datum point.
Use two sentinels, starting from both sides of the array, and finding the non-qualifying points to stop immediately. When two Sentinels stop, the two points corresponding to the Sentinel are exchanged.
When two sentinels meet, the corresponding number of datum points and Sentinels is exchanged.
It can be seen that the quick sort is at worst the time complexity is O (N2), but it is much faster than bubbling sort, because the bubble sort is just swapping the points on both sides of the adjacent bit fast sort but the jumping exchange.
1#include <stdio.h>2#include <stdlib.h>3 inta[8]={1,3,3, the, at,214,5,3};4 voidQuicksortintStartintOver )5 {6 intkey=A[start],change;7 intx=start,y=Over ;8 if(x>=y)return;9 while(1)Ten { One if(a[y]>key) Ay--; - Else if(a[x]<=key) -X + +; the Else - { -Change=A[x]; -a[x]=A[y]; +a[y]=Change ; - } + if(x==y) Break; A } ata[start]=A[x]; -a[x]=key; -Quicksort (start,x-1); -Quicksort (x+1, over); - } - intMain () in { - inti; toQuicksort0,7); + for(i=0;i<8; i++) -printf"%d", A[i]); the return 0; *}
Quick Sort C language implementation