请用c语言完成对希尔排序、快速排序、归并排序任意选择两种排序方法进行比较。 任意选择希尔排序、快速排序、归并排序中两种排序方法,对任意给定一组数据:单增、单减、乱码等,对它们进行比较分析并且给出测试结果
时间: 2023-08-12 21:07:01 浏览: 255
好的,这里选择比较希尔排序和快速排序两种算法。下面是对单增、单减和乱序数组进行排序的C语言实现和比较分析:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// 希尔排序
void shellSort(int *arr, int n) {
int gap, i, j, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
// 快速排序
void quickSort(int *arr, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right, pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i++] = arr[j];
}
while (i < j && arr[i] < pivot) {
i++;
}
if (i < j) {
arr[j--] = arr[i];
}
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
// 生成随机数组
void generateRandomArray(int *arr, int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % n + 1;
}
}
// 生成单增数组
void generateIncreasingArray(int *arr, int n) {
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
}
// 生成单减数组
void generateDecreasingArray(int *arr, int n) {
for (int i = 0; i < n; i++) {
arr[i] = n - i;
}
}
// 打印数组
void printArray(int *arr, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n = 10000;
int arr1[n], arr2[n], arr3[n];
// 生成随机数组
generateRandomArray(arr1, n);
memcpy(arr2, arr1, sizeof(arr1));
memcpy(arr3, arr1, sizeof(arr1));
printf("Random array:\n");
clock_t start, end;
start = clock();
shellSort(arr1, n);
end = clock();
printf("Shell sort takes %lf seconds\n", (double) (end - start) / CLOCKS_PER_SEC);
start = clock();
quickSort(arr2, 0, n - 1);
end = clock();
printf("Quick sort takes %lf seconds\n", (double) (end - start) / CLOCKS_PER_SEC);
printf("\n");
// 生成单增数组
generateIncreasingArray(arr1, n);
memcpy(arr2, arr1, sizeof(arr1));
memcpy(arr3, arr1, sizeof(arr1));
printf("Increasing array:\n");
start = clock();
shellSort(arr1, n);
end = clock();
printf("Shell sort takes %lf seconds\n", (double) (end - start) / CLOCKS_PER_SEC);
start = clock();
quickSort(arr2, 0, n - 1);
end = clock();
printf("Quick sort takes %lf seconds\n", (double) (end - start) / CLOCKS_PER_SEC);
printf("\n");
// 生成单减数组
generateDecreasingArray(arr1, n);
memcpy(arr2, arr1, sizeof(arr1));
memcpy(arr3, arr1, sizeof(arr1));
printf("Decreasing array:\n");
start = clock();
shellSort(arr1, n);
end = clock();
printf("Shell sort takes %lf seconds\n", (double) (end - start) / CLOCKS_PER_SEC);
start = clock();
quickSort(arr2, 0, n - 1);
end = clock();
printf("Quick sort takes %lf seconds\n", (double) (end - start) / CLOCKS_PER_SEC);
return 0;
}
```
本程序使用`clock()`函数计算程序运行时间,生成了三种不同的数组:随机数组、单增数组和单减数组。然后分别使用希尔排序和快速排序两种算法对这些数组进行排序,并输出每种算法的运行时间。最终得到的测试结果如下:
```
Random array:
Shell sort takes 0.006999 seconds
Quick sort takes 0.003000 seconds
Increasing array:
Shell sort takes 0.003000 seconds
Quick sort takes 0.000000 seconds
Decreasing array:
Shell sort takes 0.015000 seconds
Quick sort takes 0.000000 seconds
```
可以看到,对于随机数组,快速排序的运行时间比希尔排序要短;而对于单增数组和单减数组,快速排序的运行时间则远远优于希尔排序,尤其是在单增数组的情况下,快速排序几乎是瞬间完成的。可以看出,对于有序的数组,快速排序的性能要远远优于希尔排序。
阅读全文