1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <stdio.h> void select_sort(int a[],int n); void bubble_sort(int a[],int n); void point_arr(int a[], int n); int main() { int a[20] = {16,84,84,16,2,74,26,61,8,2,28,99,84,74,62,2,26,88,45,84}; int b[20] = {26,84,84,86,2,84,26,81,8,84,78,49,84,44,62,20,26,88,45,84}; printf("冒炮排序排序之前:\n"); point_arr(a, 20); bubble_sort(a, 20); printf("冒炮排序排序之后:\n"); point_arr(a, 20);
printf("选择排序排序之前:\n"); point_arr(b, 20); select_sort(b, 20); printf("选择排序排序之后:\n"); point_arr(b, 20);
return 0; } void point_arr(int a[], int n) { for (int i = 0; i < n; ++i) { printf("%d,", a[i]); } printf("\n"); }
void select_sort(int a[],int n) { for(int i=0; i<n-1; i++) { int min_index = i; for(int j=i+1; j<n; j++) { if(a[j] < a[min_index]) { min_index = j; } } if( i != min_index) { int temp = a[i]; a[i] = a[min_index]; a[min_index] = temp; } } }
void bubble_sort(int a[],int n) { for(int i=0; i<n-1; i++) { for(int j=0; j<n-1-i; j++) { if(a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1]=temp; } } } }
|