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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| #include <stdio.h> #include <stdlib.h> #include <time.h>
#define SIZE 1005
void generateRandomArray(int array[], int size) { srand(time(NULL)); for (int i = 0; i < size; i++) { array[i] = rand() % 10000; } }
void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; }
void bubbleSort(int array[], int size, int* compCount, int* moveCount) { for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { (*compCount)++; if (array[j] > array[j + 1]) { (*moveCount) += 3; swap(&array[j], &array[j + 1]); } } } }
void insertionSort(int array[], int size, int* compCount, int* moveCount) { for (int i = 1; i < size; i++) { int key = array[i]; int j = i - 1; while (j >= 0 && array[j] > key) { (*compCount)++; array[j + 1] = array[j]; (*moveCount)++; j--; } array[j + 1] = key; (*moveCount)++; } }
void selectionSort(int array[], int size, int* compCount, int* moveCount) { for (int i = 0; i < size - 1; i++) { int minIndex = i; for (int j = i + 1; j < size; j++) { (*compCount)++; if (array[j] < array[minIndex]) { minIndex = j; } } if (minIndex != i) { (*moveCount) += 3; swap(&array[i], &array[minIndex]); } } }
void quickSort(int array[], int low, int high, int* compCount, int* moveCount) { if (low < high) { int pivot = array[high]; int i = low - 1; for (int j = low; j <= high - 1; j++) { (*compCount)++; if (array[j] < pivot) { i++; (*moveCount) += 3; swap(&array[i], &array[j]); } } (*moveCount) += 3; swap(&array[i + 1], &array[high]); int partitionIndex = i + 1;
quickSort(array, low, partitionIndex - 1, compCount, moveCount); quickSort(array, partitionIndex + 1, high, compCount, moveCount); } }
void shellSort(int array[], int size, int* compCount, int* moveCount) { for (int gap = size / 2; gap > 0; gap /= 2) { for (int i = gap; i < size; i++) { int temp = array[i]; int j; for (j = i; j >= gap && array[j - gap] > temp; j -= gap) { (*compCount)++; array[j] = array[j - gap]; (*moveCount)++; } array[j] = temp; (*moveCount)++; } } }
void heapify(int array[], int size, int i, int* compCount, int* moveCount) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2;
if (left < size && array[left] > array[largest]) { largest = left; } (*compCount)++;
if (right < size && array[right] > array[largest]) { largest = right; } (*compCount)++;
if (largest != i) { (*moveCount) += 3; swap(&array[i], &array[largest]);
heapify(array, size, largest, compCount, moveCount); } }
void heapSort(int array[], int size, int* compCount, int* moveCount) { for (int i = size / 2 - 1; i >= 0; i--) { heapify(array, size, i, compCount, moveCount); }
for (int i = size - 1; i >= 0; i--) { (*moveCount) += 3; swap(&array[0], &array[i]);
heapify(array, i, 0, compCount, moveCount); } }
int main() { int array[SIZE];
for (int i = 0; i < 5; i++) { generateRandomArray(array, SIZE);
int compCount = 0; int moveCount = 0;
printf("输入数组 %d:\n", i + 1);
int bubbleArray[SIZE]; for (int j = 0; j < SIZE; j++) { bubbleArray[j] = array[j]; } bubbleSort(bubbleArray, SIZE, &compCount, &moveCount); printf("冒泡排序 - 比较: %d, 移动: %d\n", compCount, moveCount);
int insertionArray[SIZE]; for (int j = 0; j < SIZE; j++) { insertionArray[j] = array[j]; } compCount = 0; moveCount = 0; insertionSort(insertionArray, SIZE, &compCount, &moveCount); printf("插入排序 - 比较: %d, 移动: %d\n", compCount, moveCount);
int selectionArray[SIZE]; for (int j = 0; j < SIZE; j++) { selectionArray[j] = array[j]; } compCount = 0; moveCount = 0; selectionSort(selectionArray, SIZE, &compCount, &moveCount); printf("选择排序 - 比较: %d, 移动: %d\n", compCount, moveCount);
int quickArray[SIZE]; for (int j = 0; j < SIZE; j++) { quickArray[j] = array[j]; } compCount = 0; moveCount = 0; quickSort(quickArray, 0, SIZE - 1, &compCount, &moveCount); printf("快速排序 - 比较: %d, 移动: %d\n", compCount, moveCount);
int shellArray[SIZE]; for (int j = 0; j < SIZE; j++) { shellArray[j] = array[j]; } compCount = 0; moveCount = 0; shellSort(shellArray, SIZE, &compCount, &moveCount); printf("希尔排序 - 比较: %d, 移动: %d\n", compCount, moveCount);
int heapArray[SIZE]; for (int j = 0; j < SIZE; j++) { heapArray[j] = array[j]; } compCount = 0; moveCount = 0; heapSort(heapArray, SIZE, &compCount, &moveCount); printf("堆排序 - 比较: %d, 移动: %d\n", compCount, moveCount);
printf("\n"); }
return 0; }
|