随机产生300个整数构成的序列,分别以直接插入、冒泡、希尔、快速、归并等排序算法排序,并统计各自的比较次数。用c语言运行
时间: 2023-08-21 16:05:02 浏览: 69
好的,我可以为您提供一个C语言程序来完成您的要求。以下是程序代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LENGTH 300
void print_array(int* arr, int n);
void insertion_sort(int* arr, int n, int* comp_count);
void bubble_sort(int* arr, int n, int* comp_count);
void shell_sort(int* arr, int n, int* comp_count);
void quick_sort(int* arr, int left, int right, int* comp_count);
void merge_sort(int* arr, int left, int right, int* comp_count);
void merge(int* arr, int left, int mid, int right, int* comp_count);
int main()
{
int arr[LENGTH];
int i, comp_count;
srand(time(NULL));
for (i = 0; i < LENGTH; i++) {
arr[i] = rand() % 100;
}
printf("Original array:\n");
print_array(arr, LENGTH);
comp_count = 0;
insertion_sort(arr, LENGTH, &comp_count);
printf("After insertion sort:\n");
print_array(arr, LENGTH);
printf("Comparison count: %d\n\n", comp_count);
comp_count = 0;
bubble_sort(arr, LENGTH, &comp_count);
printf("After bubble sort:\n");
print_array(arr, LENGTH);
printf("Comparison count: %d\n\n", comp_count);
comp_count = 0;
shell_sort(arr, LENGTH, &comp_count);
printf("After shell sort:\n");
print_array(arr, LENGTH);
printf("Comparison count: %d\n\n", comp_count);
comp_count = 0;
quick_sort(arr, 0, LENGTH - 1, &comp_count);
printf("After quick sort:\n");
print_array(arr, LENGTH);
printf("Comparison count: %d\n\n", comp_count);
comp_count = 0;
merge_sort(arr, 0, LENGTH - 1, &comp_count);
printf("After merge sort:\n");
print_array(arr, LENGTH);
printf("Comparison count: %d\n\n", comp_count);
return 0;
}
void print_array(int* arr, int n)
{
int i;
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void insertion_sort(int* arr, int n, int* comp_count)
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
arr[j + 1] = arr[j];
(*comp_count)++;
}
arr[j + 1] = temp;
}
}
void bubble_sort(int* arr, int n, int* comp_count)
{
int i, j, temp;
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;
(*comp_count)++;
}
}
}
}
void shell_sort(int* arr, int n, int* comp_count)
{
int i, j, gap, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j];
(*comp_count)++;
}
arr[j + gap] = temp;
}
}
}
void quick_sort(int* arr, int left, int right, int* comp_count)
{
int i, j, pivot, temp;
if (left < right) {
pivot = arr[left];
i = left;
j = right;
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
(*comp_count)++;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < pivot) {
i++;
(*comp_count)++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = pivot;
quick_sort(arr, left, i - 1, comp_count);
quick_sort(arr, i + 1, right, comp_count);
}
}
void merge_sort(int* arr, int left, int right, int* comp_count)
{
int mid;
if (left < right) {
mid = (left + right) / 2;
merge_sort(arr, left, mid, comp_count);
merge_sort(arr, mid + 1, right, comp_count);
merge(arr, left, mid, right, comp_count);
}
}
void merge(int* arr, int left, int mid, int right, int* comp_count)
{
int i, j, k, temp[right - left + 1];
i = left;
j = mid + 1;
k = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
(*comp_count)++;
}
else {
temp[k++] = arr[j++];
(*comp_count)++;
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = left, k = 0; i <= right; i++, k++) {
arr[i] = temp[k];
}
}
```
这个程序会生成一个长度为300的随机整数序列,并分别使用直接插入、冒泡、希尔、快速、归并等排序算法对其进行排序,并统计每个算法的比较次数。如果您想要使用其他排序算法,可以自行添加函数实现。
阅读全文