Xiabodan Source: Http://blog.csdn.net/xiabodan
Sorting Algorithms (sorting algorithm) is an integral part of computer algorithms. is also part of the program = algorithm + data structure (algorithm).
Experimental platform: Raspberry 2 B + Ubuntu Mate
Insert Sort
Insert sort//stable//o (n^2) comparisons and Swaps//adaptive:o (N) time time nearly sorted//very low overheadvoid insertion (ele Menttype a[],int N) {int p = 0; int j = 0; for (p=1;p<n;p++) {ElementType tem = a[p]; for (j=p;j>0&&a[j-1]> tem;j--) {A[j] = a[j-1];} A[J] = tem;}}
Hill sort
Hill sort//o (N^3/2) unstable//adaptive:o (N.LG (N)) time time nearly sorted//very low overheadvoid shell (ElementType a[ ],int n) {int i,j,inc;elementtype tem;for (inc=n/2;inc>0;inc/=2) {for (i=inc;i<n;i++) {tem = A[i];for (j=i;j>= Inc;j-=inc) {if (Tem<a[j-inc]) a[j] = A[j-inc];elsebreak;} A[J] = tem;}}}
Bubble sort
Bubble sort//o (n^2) stable//adaptive:o (N) time time nearly sorted//very low overheadvoid bubble (ElementType a[],int N) { int flag = 1;int I,j;for (i=0;i<n;i++) {flag = 0;for (j=n-1;j>i;j--) {if (A[j]<a[j-1]) {flag = 1;swap (a+j,a+j-1);}} if (flag = = 0) break;}}
Select sort
Select Sort//not stable//o (1) Extra space//θ (n2) comparisons//θ (n) swaps//not adaptivevoid selection (ElementType a[],int N) { int I,j;int k;for (i=0;i<n;i++) {k = I;for (j=i+1;j<n;j++) {if (A[j]<a[k]) {k = j;}} Swap (A+I,A+K);}}
Quick Sort
//Quick Sort//not stable//o (LG (n)) extra space (see discussion)//o (N2) time, but typically O (n LG (n)) Time//not adaptive#define CUT 3element Type MEDIAN3 (ElementType a[],int left, int right) {int center = (left +right)/2;if (A[left]>a[center]) swap (&a[left ],&a[center]), if (A[left]>a[right]) swap (&a[left],&a[right]), if (A[center]>a[right]) swap (&A [Center],&a[right]); swap (&a[center],&a[right-1]); return a[right-1];} void Qsort (ElementType A[],int left, int. right) {int. I,j;elementtype pivot;if (left + cut<= right) {pivot = MEDIAN3 (a,left , right); Select middle Element as Pivoti = Left;j = Right-1;for (;;) {while (A[++i]<pivot) {}while (A[--j]>pivot) {}if (i<j) swap (&a[i],&a[j]); elsebreak;} Swap (&a[i],&a[right-1]); Qsort (a,left,i-1); Qsort (a,i+1,right);} Elseinsertion (a+left,right-left+1);} void Quick1 (ElementType a[],int N) {Qsort (a,0,n-1);}
To be continued .....
Summary:
Bubbling and inserting is to slowly find the largest or smallest put on the first go; choose to directly find the maximum or minimum to the first; Merge and Devide-merge-conquer (divide and conquer strategy), the period will use the above mentioned the most basic algorithm; heap sorting uses the idea of choosing sort The bucket sort uses the space to change the time method; original aim.
Reference:
data structure and algorithm analysis- C language Description [M], mechanical industry press
Blog Park Vamei's blog: http://www.cnblogs.com/vamei/archive/2013/03/12/2948847.html
Tianjin City College A fine course: http://sjjp.tjuci.edu.cn/sjjg/datastructure/ds/web/paixu/paixu8.1.1.1.htm
a sort of foreign site has animation, analysis, for code: http://www.sorting-algorithms.com/
Data structure and algorithm analysis-sorting