C 语言实现六种内部排序算法性能大比拼:含代码与注意要点
以下是使用 C 语言实现的内部排序算法比较的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> // 交换两个元素 void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // 打印数组 void printArray(int arr[], int size) { for (int i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } // 直接插入排序 void insertionSort(int arr[], int n, int* compCount, int* moveCount) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; (*compCount)++; (*moveCount) += 3; } arr[j + 1] = key; (*moveCount) += 3; } } // 希尔排序 void shellSort(int arr[], int n, int* compCount, int* moveCount) { int gap, i, j, temp; for (gap = n / 2; gap > 0; gap = gap / 2) { for (i = gap; i < n; i++) { temp = arr[i]; j = i; while (j >= gap && arr[j - gap] > temp) { arr[j] = arr[j - gap]; j -= gap; (*compCount)++; (*moveCount) += 3; } arr[j] = temp; (*moveCount) += 3; } } } // 冒泡排序 void bubbleSort(int arr[], int n, int* compCount, int* moveCount) { int i, j; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { swap(&arr[j], &arr[j + 1]); (*compCount)++; (*moveCount) += 3; } } } } // 快速排序 int partition(int arr[], int low, int high, int* compCount, int* moveCount) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j] <= pivot) { i++; swap(&arr[i], &arr[j]); (*compCount)++; (*moveCount) += 3; } } swap(&arr[i + 1], &arr[high]); (*moveCount) += 3; return (i + 1); } void quickSort(int arr[], int low, int high, int* compCount, int* moveCount) { if (low < high) { int pi = partition(arr, low, high, compCount, moveCount); quickSort(arr, low, pi - 1, compCount, moveCount); quickSort(arr, pi + 1, high, compCount, moveCount); } } // 归并排序 void merge(int arr[], int l, int m, int r, int* compCount, int* moveCount) { int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (int i = 0; i < n1; i++) L[i] = arr[l + i]; for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; int i = 0, j = 0, k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k++] = L[i++]; (*compCount)++; (*moveCount) += 3; } else { arr[k++] = R[j++]; (*compCount)++; (*moveCount) += 3; } } while (i < n1) { arr[k++] = L[i++]; (*moveCount) += 3; } while (j < n2) { arr[k++] = R[j++]; (*moveCount) += 3; } } void mergeSort(int arr[], int l, int r, int* compCount, int* moveCount) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m, compCount, moveCount); mergeSort(arr, m + 1, r, compCount, moveCount); merge(arr, l, m, r, compCount, moveCount); } } // 简单选择排序 void selectionSort(int arr[], int n, int* compCount, int* moveCount) { int i, j, min_idx; for (i = 0; i < n - 1; i++) { min_idx = i; for (j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) min_idx = j; (*compCount)++; } if (min_idx!= i) { swap(&arr[i], &arr[min_idx]); (*moveCount) += 3; } } } // 生成随机测试数据 void generateRandomData(int arr[], int n) { srand(time(NULL)); for (int i = 0; i < n; i++) arr[i] = rand(); } // 比较各种排序算法的性能 void compareSortingAlgorithms() { int choice, ascending; int arr[60]; int compCount = 0, moveCount = 0; printf("1. 直接插入排序\n"); printf("2. 希尔排序\n"); printf("3. 冒泡排序\n"); printf("4. 快速排序\n"); printf("5. 归并排序\n"); printf("6. 简单选择排序\n"); printf("请选择排序算法: "); scanf("%d", &choice); printf("1. 升序\n"); printf("2. 降序\n"); printf("请选择排序顺序: "); scanf("%d", &ascending); generateRandomData(arr, 60); switch (choice) { case 1: insertionSort(arr, 60, &compCount, &moveCount); break; case 2: shellSort(arr, 60, &compCount, &moveCount); break; case 3: bubbleSort(arr, 60, &compCount, &moveCount); break; case 4: quickSort(arr, 0, 59, &compCount, &moveCount); break; case 5: mergeSort(arr, 0, 59, &compCount, &moveCount); break; case 6: selectionSort(arr, 60, &compCount, &moveCount); break; default: printf("无效的选择\n"); return; } if (ascending == 1) { printf("升序排序结果: "); printArray(arr, 60); } else { printf("降序排序结果: "); for (int i = 59; i >= 0; i--) printf("%d ", arr[i]); printf("\n"); } printf("排序趟数: %d\n", compCount); printf("关键字比较次数: %d\n", compCount); printf("元素移动次数: %d\n", moveCount); } int main() { compareSortingAlgorithms(); return 0; } ``` 以下是 3 点需要额外注意的地方: 1. 代码中的随机数生成可能存在一定的局限性,在实际应用中可能需要更复杂的随机数生成方法。 2. 对于不同规模的数据,各种排序算法的性能表现可能会有所不同,可以进一步测试和分析。 3. 代码的可扩展性方面,可以考虑将排序算法封装为单独的函数,以便于后续的维护和修改。 ######[AI写代码神器 | 2078点数解答 | 2024-12-21 21:46:21]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)338
- 工程师必知:软件系统用户文档编写九大要点揭秘(百度文心 | 350点数解答 | 2023-11-09 18:23:53)178
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)306
- C++ 破解“国王游戏”弱化版:贪心排序算法巧求最少最大奖赏(DeepSeek | 550点数解答 | 2025-04-28 18:16:50)392
- 巧妙贪心排序:解决国王游戏中大臣奖赏最大化问题(DeepSeek | 1002点数解答 | 2025-05-10 20:58:15)190
- C++ 实现:用贪心算法解国王游戏(弱化版),使大臣获赏最大值最小(DeepSeek | 507点数解答 | 2025-05-10 20:59:01)172
- 揭秘Java开发中监视器内部的线程同步机制(百度文心 | 291点数解答 | 2023-11-09 17:16:49)264
- 页面兼具内、嵌、外三种 CSS 样式,却遇服务器繁忙提示! (GPT | 15点数解答 | 2024-06-07 00:10:01)251
- 字符数组实现字符串匹配算法:模拟内部操作的奥秘(字节豆包 | 23点数解答 | 2024-12-15 12:38:07)221
- C 语言实现字符串匹配:含边界处理及 \0 把控,精准输出匹配序号!(字节豆包 | 422点数解答 | 2024-12-15 12:39:10)233
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)446
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)350