选择排序
void SelectSort(int *a,int n){ int i,j; int min; for (i=0;i< n-1;++i) { min = i; for (j=i+1;j
冒泡排序
void BubbleSort(int *a,int n){ int i,j; for (i=0;i< n-1;++i) { for (j=0;j< n-1-i;++j) { if (a[j]>a[j+1]) swap(a[j],a[j+1]); } }}void BubbleSortBetter(int *a,int n){ int i,j; bool flag; for (i=0;i< n-1;++i) { flag =true; for (j=0;j< n-1-i;++j) { if (a[j]>a[j+1]) { swap(a[j],a[j+1]); flag =false; } } if (flag) break; }}void BubbleSortBest(int *a,int n){ int exchange = n-1; while (exchange) { int bound = exchange; exchange = 0; for (int i=0;ia[i+1]) { swap(a[i],a[i+1]); exchange = i; } } }}
总结:
参考资料: