随机产生300个整数构成的序列,分别以直接插入,冒泡,希尔,快速,归并等排列算法排序,并统计各自比较次数。用c语言实现
时间: 2023-08-11 21:07:37 浏览: 80
以下是用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print_array(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void direct_insertion_sort(int arr[], int size, int *comp_count) {
for (int i = 1; i < size; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
(*comp_count)++;
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
void bubble_sort(int arr[], int size, int *comp_count) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
(*comp_count)++;
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void shell_sort(int arr[], int size, int *comp_count) {
int gap;
for (gap = size / 2; gap > 0; gap /= 2) {
for (int i = gap; i < size; i++) {
int temp = arr[i];
int j;
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
(*comp_count)++;
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
}
}
void quick_sort(int arr[], int left, int right, int *comp_count) {
if (left < right) {
int i = left, j = right, pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
(*comp_count)++;
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < pivot) {
(*comp_count)++;
i++;
}
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(int arr[], int left, int mid, int right, int *comp_count) {
int i = left, j = mid + 1, k = 0;
int *temp = (int *)malloc((right - left + 1) * sizeof(int));
while (i <= mid && j <= right) {
(*comp_count)++;
if (arr[i] <= arr[j]) {
temp[k] = arr[i];
i++;
} else {
temp[k] = arr[j];
j++;
}
k++;
}
while (i <= mid) {
temp[k] = arr[i];
i++;
k++;
}
while (j <= right) {
temp[k] = arr[j];
j++;
k++;
}
for (int p = 0; p < k; p++) {
arr[left + p] = temp[p];
}
free(temp);
}
void merge_sort(int arr[], int left, int right, int *comp_count) {
if (left < right) {
int 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);
}
}
int main() {
int arr[300];
srand(time(NULL));
for (int i = 0; i < 300; i++) {
arr[i] = rand() % 1000;
}
printf("Original array:\n");
print_array(arr, 300);
int direct_insertion_comp_count = 0;
int bubble_comp_count = 0;
int shell_comp_count = 0;
int quick_comp_count = 0;
int merge_comp_count = 0;
int direct_insertion_arr[300];
int bubble_arr[300];
int shell_arr[300];
int quick_arr[300];
int merge_arr[300];
for (int i = 0; i < 300; i++) {
direct_insertion_arr[i] = arr[i];
bubble_arr[i] = arr[i];
shell_arr[i] = arr[i];
quick_arr[i] = arr[i];
merge_arr[i] = arr[i];
}
direct_insertion_sort(direct_insertion_arr, 300, &direct_insertion_comp_count);
bubble_sort(bubble_arr, 300, &bubble_comp_count);
shell_sort(shell_arr, 300, &shell_comp_count);
quick_sort(quick_arr, 0, 299, &quick_comp_count);
merge_sort(merge_arr, 0, 299, &merge_comp_count);
printf("Direct Insertion Sort:\n");
print_array(direct_insertion_arr, 300);
printf("Comparison count: %d\n", direct_insertion_comp_count);
printf("Bubble Sort:\n");
print_array(bubble_arr, 300);
printf("Comparison count: %d\n", bubble_comp_count);
printf("Shell Sort:\n");
print_array(shell_arr, 300);
printf("Comparison count: %d\n", shell_comp_count);
printf("Quick Sort:\n");
print_array(quick_arr, 300);
printf("Comparison count: %d\n", quick_comp_count);
printf("Merge Sort:\n");
print_array(merge_arr, 300);
printf("Comparison count: %d\n", merge_comp_count);
return 0;
}
```
这段代码会生成一个300个随机整数的数组,并对其进行5种排序算法的排序,并统计各自的比较次数。
阅读全文