The insertion sort is the most basic sort method, with a time complexity of O (Nlogn). High efficiency, and in the written interview is often asked, to write more practice to be able to readily write the goal of the fast row.
The quick sort is a sort of division exchange proposed by C.r.a.hoare in 1962. It adopts a strategy of division, which is usually referred to as the division method (Divide-and-conquermethod).
The basic idea of this method is:
1. First, a number is taken from the series as the base number. (Here we select the first element of the array each time)
2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.
3. Repeat the second step for the left and right intervals until there is only one number for each interval.
Post an easy-to-understand link: http://blog.csdn.net/morewindows/article/details/6684558
The simple implementation of the insertion in the C language is sorted as follows:
1 /*C language implementation of fast sorting algorithm*/2 /*S is the array to be sorted, and L is the starting position to sort the array ,*/3 4 voidQuick_sort (intS[],intLintR)5 {6 if(l<r) {7 inti = l, j = r, x =S[l];8 while(i<j)9 {Ten while(i<j&&s[j]>=x) { Onej--; A } - if(i<j) { -s[i++] =S[j]; the } - while(i<j&&s[i]<=x) { -i++; - } + if(i<j) { -s[j--] =S[i]; + } A } atS[i] =x; -Quick_sort (S, l, I-1); -Quick_sort (S, i+1, R); - } -}
A simple test case with the following code:
#include <stdio.h>#defineMAXSIZE 10/*C language implementation of fast sorting algorithm*//*S is the array to be sorted, and L is the starting position to sort the array ,*/voidQuick_sort (intS[],intLintR) { if(l<r) {inti = l, j = r, x =S[l]; while(i<j) { while(i<j&&s[j]>=x) {J--; } if(i<j) {S[i++] =S[j]; } while(i<j&&s[i]<=x) {i++; } if(i<j) {S[j--] =S[i]; }} S[i]=x; Quick_sort (S, l, I-1); Quick_sort (S, I+1, R); }}intMain () {intA[maxsize]; inti; printf ("Please input the num:\n"); for(i=0; i<maxsize; i++) {scanf ("%d",&A[i]); } printf ("before the sort:\n"); for(i=0; i<maxsize; i++) {printf ("%d", A[i]); } printf ("\ n"); Quick_sort (A,0, MAXSIZE); printf ("After the sort:\n"); for(i=0; i<maxsize; i++) {printf ("%d", A[i]); } printf ("\ n");}
View Code
Fast sequencing of basic algorithms