标签:
插入排序
1 void InsertSort(int a[], int n) 2 { 3 int in,out,t; 4 for(out=1; out<n; out++) 5 { 6 t = a[out]; 7 in = out; 8 while(in>0 && a[in-1]>=t) 9 { 10 a[in] = a[in-1]; 11 in--; 12 } 13 a[in] = t; 14 } 15 }
冒泡排序
1 void BubbleSort(int *a, int n) 2 { 3 int i,j,t; 4 for(i=0; i<n-1; i++) 5 { 6 for(j=0; j<n-1-i; j++) 7 { 8 if(a[j]>a[j+1]) 9 { 10 t = a[j]; 11 a[j] = a[j+1]; 12 a[j+1] = t; 13 } 14 } 15 } 16 }
选择排序
1 void SelectSort(int *a, int n) 2 { 3 int i,j,m,t; 4 for(i=0; i<n-1; i++) 5 { 6 m = i; 7 for(j=i+1; j<n; j++) 8 { 9 if(a[m]>a[j]) 10 { 11 m=j; 12 } 13 } 14 t=a[m]; 15 a[m] = a[i]; 16 a[i] = t; 17 } 18 }
二分查找
1 void InsertSort(int a[], int n) 2 { 3 int in,out,t; 4 for(out=1; out<n; out++) 5 { 6 t = a[out]; 7 in = out; 8 while(in>0 && a[in-1]>=t) 9 { 10 a[in] = a[in-1]; 11 in--; 12 } 13 a[in] = t; 14 } 15 }
快速排序
1 void QuickSort(int *a, int l, int r) 2 { 3 int i,j,t; 4 if(l <r) 5 { 6 i=l; 7 j=r; 8 t = a[i]; 9 while(i<j) 10 { 11 while(i<j && a[j]>t) j--; 12 if(i<j) 13 { 14 a[i] = a[j]; 15 i++; 16 } 17 while(i<j && a[i]<t) i++; 18 if(i<j) 19 { 20 a[j] = a[i]; 21 j--; 22 } 23 } 24 a[i] = t; 25 QuickSort(a, l, i-1); 26 QuickSort(a, i+1, r); 27 } 28 }
标签:
原文地址:http://www.cnblogs.com/adevelopersblog/p/5792403.html