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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <time.h>
void bubble_sort_basic(int arr[], int n) { printf("执行基础冒泡排序...\n"); for (int i = 0; i < n - 1; i++) { printf("第 %d 轮排序: ", i + 1); for (int j = 0; j < n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } for (int k = 0; k < n; k++) { printf("%d ", arr[k]); } printf("\n"); } }
void bubble_sort_optimized(int arr[], int n) { printf("执行优化冒泡排序...\n"); for (int i = 0; i < n - 1; i++) { bool swapped = false; printf("第 %d 轮排序: ", i + 1); for (int j = 0; j < n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = true; } } for (int k = 0; k < n; k++) { printf("%d ", arr[k]); } printf("\n"); if (!swapped) { printf("数组已有序,提前终止!\n"); break; } } }
void cocktail_sort(int arr[], int n) { printf("执行双向冒泡排序(鸡尾酒排序)...\n"); int left = 0; int right = n - 1; int round = 1; while (left < right) { bool swapped = false; printf("第 %d 轮向右冒泡: ", round); for (int i = left; i < right; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } right--; for (int k = 0; k < n; k++) { printf("%d ", arr[k]); } printf("\n"); if (!swapped) break; printf("第 %d 轮向左冒泡: ", round); swapped = false; for (int i = right; i > left; i--) { if (arr[i] < arr[i - 1]) { int temp = arr[i]; arr[i] = arr[i - 1]; arr[i - 1] = temp; swapped = true; } } left++; for (int k = 0; k < n; k++) { printf("%d ", arr[k]); } printf("\n"); if (!swapped) break; round++; } }
void bubble_sort_recursive(int arr[], int n) { if (n <= 1) return; for (int i = 0; i < n - 1; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } bubble_sort_recursive(arr, n - 1); }
void performance_comparison() { printf("=== 性能比较测试 ===\n"); const int sizes[] = {100, 500, 1000}; const int num_sizes = sizeof(sizes) / sizeof(sizes[0]); for (int s = 0; s < num_sizes; s++) { int size = sizes[s]; printf("\n数组大小: %d\n", size); int* arr1 = (int*)malloc(size * sizeof(int)); int* arr2 = (int*)malloc(size * sizeof(int)); srand((unsigned int)time(NULL)); for (int i = 0; i < size; i++) { arr1[i] = rand() % 1000; arr2[i] = arr1[i]; } clock_t start = clock(); bubble_sort_basic(arr1, size); clock_t end = clock(); double time_basic = ((double)(end - start)) / CLOCKS_PER_SEC; start = clock(); bubble_sort_optimized(arr2, size); end = clock(); double time_optimized = ((double)(end - start)) / CLOCKS_PER_SEC; printf("基础版本用时: %.6f 秒\n", time_basic); printf("优化版本用时: %.6f 秒\n", time_optimized); printf("性能提升: %.2f%%\n", ((time_basic - time_optimized) / time_basic) * 100); free(arr1); free(arr2); } }
bool is_sorted(int arr[], int n) { for (int i = 0; i < n - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; }
void print_array(int arr[], int size, const char* title) { printf("%s: ", title); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); }
void test_bubble_sort_algorithms() { printf("=== 冒泡排序算法测试 ===\n\n"); int test_data[] = {64, 34, 25, 12, 22, 11, 90, 88, 76, 50}; int size = sizeof(test_data) / sizeof(test_data[0]); printf("1. 基础冒泡排序测试\n"); int* arr1 = (int*)malloc(size * sizeof(int)); for (int i = 0; i < size; i++) arr1[i] = test_data[i]; print_array(arr1, size, "排序前"); bubble_sort_basic(arr1, size); print_array(arr1, size, "排序后"); printf("排序正确性: %s\n\n", is_sorted(arr1, size) ? "正确" : "错误"); free(arr1); printf("2. 优化冒泡排序测试\n"); int* arr2 = (int*)malloc(size * sizeof(int)); for (int i = 0; i < size; i++) arr2[i] = test_data[i]; print_array(arr2, size, "排序前"); bubble_sort_optimized(arr2, size); print_array(arr2, size, "排序后"); printf("排序正确性: %s\n\n", is_sorted(arr2, size) ? "正确" : "错误"); free(arr2); printf("3. 双向冒泡排序测试\n"); int* arr3 = (int*)malloc(size * sizeof(int)); for (int i = 0; i < size; i++) arr3[i] = test_data[i]; print_array(arr3, size, "排序前"); cocktail_sort(arr3, size); print_array(arr3, size, "排序后"); printf("排序正确性: %s\n\n", is_sorted(arr3, size) ? "正确" : "错误"); free(arr3); printf("4. 递归冒泡排序测试\n"); int* arr4 = (int*)malloc(size * sizeof(int)); for (int i = 0; i < size; i++) arr4[i] = test_data[i]; print_array(arr4, size, "排序前"); bubble_sort_recursive(arr4, size); print_array(arr4, size, "排序后"); printf("排序正确性: %s\n\n", is_sorted(arr4, size) ? "正确" : "错误"); free(arr4); }
int main() { test_bubble_sort_algorithms(); return 0; }
|