对随机产生的1000个整数构成的序列分别进行直接插入、冒泡、希尔、快速、归并排序,并统计各自的比较次数。C语言
时间: 2023-07-10 20:28:02 浏览: 89
编程实现希尔、快速、堆、归并排序算法,随机产生10000个随机数存入磁盘文件,读取,并将不同排序方法的结果存入文件中(含报告)
5星 · 资源好评率100%
好的,我会根据您的要求回答问题,并且不会透露您的任何要求。关于您的问题,我可以提供以下代码来实现对随机生成的1000个整数进行直接插入、冒泡、希尔、快速、归并排序,并统计各自的比较次数的功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int insertion_sort(int arr[], int n) {
int i, j, key, count = 0;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
count++;
}
arr[j + 1] = key;
count++;
}
return count;
}
int bubble_sort(int arr[], int n) {
int i, j, temp, count = 0;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
count++;
}
count++;
}
}
return count;
}
int shell_sort(int arr[], int n) {
int i, j, gap, key, count = 0;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
key = arr[i];
for (j = i; j >= gap && arr[j - gap] > key; j -= gap) {
arr[j] = arr[j - gap];
count++;
}
arr[j] = key;
count++;
}
}
return count;
}
int partition(int arr[], int low, int high, int* count) {
int pivot = arr[high];
int i = low - 1, j, temp;
for (j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
(*count)++;
}
(*count)++;
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
(*count)++;
return i + 1;
}
void quick_sort(int arr[], int low, int high, int* count) {
if (low < high) {
int pi = partition(arr, low, high, count);
quick_sort(arr, low, pi - 1, count);
quick_sort(arr, pi + 1, high, count);
}
}
void merge(int arr[], int l, int m, int r, int* count) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
(*count)++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
(*count)++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
(*count)++;
}
}
int merge_sort(int arr[], int l, int r, int* count) {
int count_left, count_right, count_merge;
if (l < r) {
int m = l + (r - l) / 2;
count_left = merge_sort(arr, l, m, count);
count_right = merge_sort(arr, m + 1, r, count);
merge(arr, l, m, r, &count_merge);
(*count) += count_left + count_right + count_merge;
}
return *count;
}
int main() {
int arr[1000], n = 1000, i, count;
srand(time(NULL));
printf("Original array:\n");
for (i = 0; i < n; i++) {
arr[i] = rand() % 100;
printf("%d ", arr[i]);
}
printf("\n");
count = insertion_sort(arr, n);
printf("Insertion sort:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of comparisons: %d\n", count);
count = bubble_sort(arr, n);
printf("Bubble sort:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of comparisons: %d\n", count);
count = shell_sort(arr, n);
printf("Shell sort:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of comparisons: %d\n", count);
count = 0;
quick_sort(arr, 0, n - 1, &count);
printf("Quick sort:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of comparisons: %d\n", count);
count = 0;
merge_sort(arr, 0, n - 1, &count);
printf("Merge sort:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of comparisons: %d\n", count);
return 0;
}
```
在这个程序中,我们使用了随机数生成器来生成1000个整数,然后对这些整数进行直接插入、冒泡、希尔、快速、归并排序,并统计了各自的比较次数。其中,插入排序、冒泡排序、希尔排序、快速排序、归并排序的代码实现分别为 `insertion_sort()`、`bubble_sort()`、`shell_sort()`、`quick_sort()`、`merge_sort()`。
需要注意的是,为了统计比较次数,我们在排序函数中添加了一个 `count` 变量,并在每次比较时递增该变量的值。如果需要统计每个排序算法的运行时间,可以使用 `clock()` 函数来计算程序的运行时间。
希望这个代码能够对您有所帮助!
阅读全文