//直接插入排序函数模板 template <class T> void insertionSort(T a[], int n) { int i, ; T temp; for (int i = 1; i < n; i++) { int j = i; T temp = a[i]; while (j > 0 && temp < a[j-1]) { a[j] = a[j - 1]; j--; } a[j] = temp; } } //简单选择排序函数模板 template <class T> void mySwap(T &x, T &y) { T temp = x; x = y; y = temp; } template <class T> void selectionSort(T a[], int n) { for (int i = 0; i < n - 1; i++) { int leastIndex = i; for (int j = i + 1; j < n; j++) { if (a[j] < a[leastIndex]) { leastIndex = j; } } mySwap(a[i], a[leastIndex]); } } //冒泡排序 template <class T> void bubbleSort(T a[], int n) { int i = n - 1; while (i > 0) { int lastExchangeIndex = 0; for (int j = 0; j < i; j++) { if (a[j + 1] < a[j]) mySwap(a[j], a[j + 1]); lastExchangeIndex = j; } i = lastExchangeIndex; } }
怕什么真理无穷,进一寸有一寸的欢喜。---胡适