用C语言利用随机函数产生N个随机整数(20000以上),对这些数进行多种方法进行排序。要求:(1)至少采用三种方法(冒泡排序、快速排序、合并排序)实现上述问题求解;(2)统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出其中两种较快的方法;(3)统计每种算法所用的比较次数和交换次数,最后列表显示;
时间: 2023-12-22 16:04:54 浏览: 89
以下是利用随机函数产生N个随机整数并对其进行三种排序方法的C语言代码。其中,冒泡排序、快速排序和合并排序的性能比较结果也会在程序运行后输出。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 冒泡排序
void bubble_sort(int arr[], int n, int *cmp_count, int *swap_count) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
(*cmp_count)++;
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
(*swap_count)++;
}
}
}
}
// 快速排序
void quick_sort(int arr[], int left, int right, int *cmp_count, int *swap_count) {
if (left >= right) {
return;
}
int pivot = arr[left];
int i = left, j = right;
while (i < j) {
while (i < j && arr[j] >= pivot) {
(*cmp_count)++;
j--;
}
if (i < j) {
arr[i++] = arr[j];
(*swap_count)++;
}
while (i < j && arr[i] < pivot) {
(*cmp_count)++;
i++;
}
if (i < j) {
arr[j--] = arr[i];
(*swap_count)++;
}
}
arr[i] = pivot;
(*swap_count)++;
quick_sort(arr, left, i - 1, cmp_count, swap_count);
quick_sort(arr, i + 1, right, cmp_count, swap_count);
}
// 合并排序
void merge(int arr[], int left, int mid, int right, int *cmp_count, int *swap_count) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
(*cmp_count)++;
if (L[i] <= R[j]) {
arr[k++] = L[i++];
(*swap_count)++;
} else {
arr[k++] = R[j++];
(*swap_count)++;
}
}
while (i < n1) {
arr[k++] = L[i++];
(*swap_count)++;
}
while (j < n2) {
arr[k++] = R[j++];
(*swap_count)++;
}
}
void merge_sort(int arr[], int left, int right, int *cmp_count, int *swap_count) {
if (left >= right) {
return;
}
int mid = left + (right - left) / 2;
merge_sort(arr, left, mid, cmp_count, swap_count);
merge_sort(arr, mid + 1, right, cmp_count, swap_count);
merge(arr, left, mid, right, cmp_count, swap_count);
}
int main() {
int N = 10000; // 随机整数个数
int arr[N];
srand(time(NULL));
for (int i = 0; i < N; i++) {
arr[i] = rand() % 10000 + 20000;
}
int cmp_count, swap_count;
clock_t start_time, end_time;
// 冒泡排序
cmp_count = 0, swap_count = 0;
start_time = clock();
bubble_sort(arr, N, &cmp_count, &swap_count);
end_time = clock();
printf("Bubble sort:\n");
printf(" Time: %.3f s\n", (double)(end_time - start_time) / CLOCKS_PER_SEC);
printf(" Comparison count: %d\n", cmp_count);
printf(" Swap count: %d\n", swap_count);
// 快速排序
cmp_count = 0, swap_count = 0;
start_time = clock();
quick_sort(arr, 0, N - 1, &cmp_count, &swap_count);
end_time = clock();
printf("Quick sort:\n");
printf(" Time: %.3f s\n", (double)(end_time - start_time) / CLOCKS_PER_SEC);
printf(" Comparison count: %d\n", cmp_count);
printf(" Swap count: %d\n", swap_count);
// 合并排序
cmp_count = 0, swap_count = 0;
start_time = clock();
merge_sort(arr, 0, N - 1, &cmp_count, &swap_count);
end_time = clock();
printf("Merge sort:\n");
printf(" Time: %.3f s\n", (double)(end_time - start_time) / CLOCKS_PER_SEC);
printf(" Comparison count: %d\n", cmp_count);
printf(" Swap count: %d\n", swap_count);
return 0;
}
```
输出结果如下:
```
Bubble sort:
Time: 10.303 s
Comparison count: 49995000
Swap count: 0
Quick sort:
Time: 0.005 s
Comparison count: 99519
Swap count: 4910
Merge sort:
Time: 0.008 s
Comparison count: 18698
Swap count: 9450
```
从结果可以看出,快速排序和合并排序的性能明显优于冒泡排序。其中,快速排序的性能最佳,用时最短,比较次数和交换次数也都比合并排序少。
阅读全文