- 快速插入排序
- void straight_insertion_sort (int v[], int n)
- {
- int i;
- for (i = 1; i<=n-1; i++) {
- if (v[i] <= v[i-1]) {
- int tmp = v[i];
- int j;
- for (j = i-1; j>=0; j--) {
- if (tmp < v[j]) {
- v[j+1] = v[j];
- } else {
- break;
- }
- }
- v[j+1] = tmp;
- }
- }
- }
- 折半插入排序
- 起泡排序
- void buble_sort (int v[], int n)
- {
- for (int i=n-2; i>=0; i--) {
- int swap_flag = 0;
- for (int j=0; j<=i; j++) {
- if (v[j] > v[j+1]) {
- int tmp = v[j];
- v[j] = v[j+1];
- v[j+1] = tmp;
- swap_flag = 1;
- }
- }
- if (swap_flag == 0) {
- break;
- }
- }
- }
- 简单选择排序
- void simple_selection_sort (int v[], int n)
- {
- for (int i=0; i<=n-2; i++) {
- for (int j=i+1; j<=n-1; j++) {
- if (v[i] > v[j]) {
- int tmp = v[j];
- v[j] = v[i];
- v[i] = tmp;
- }
- }
- }
- }