随机产生300个整数构成的序列,分别以直接插入,冒泡,希尔,快速,归并等排序算法排序,并统计各自的比较次数,用c语言实现
时间: 2023-09-22 08:07:35 浏览: 77
以下是C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 300
void print_array(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int direct_insertion_sort(int arr[], int size) {
int count = 0;
for (int i = 1; i < size; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j+1] = arr[j];
j--;
count++;
}
arr[j+1] = temp;
}
return count;
}
int bubble_sort(int arr[], int size) {
int count = 0;
for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-1-i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
count++;
}
}
}
return count;
}
int shell_sort(int arr[], int size) {
int count = 0;
for (int 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) {
arr[j+gap] = arr[j];
count++;
}
arr[j+gap] = temp;
}
}
return count;
}
int quick_sort(int arr[], int start, int end) {
int count = 0;
if (start < end) {
int pivot = arr[start];
int i = start, j = end;
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
count++;
}
if (i < j) {
arr[i++] = arr[j];
}
while (i < j && arr[i] <= pivot) {
i++;
count++;
}
if (i < j) {
arr[j--] = arr[i];
}
}
arr[i] = pivot;
count++;
quick_sort(arr, start, i-1);
quick_sort(arr, i+1, end);
}
return count;
}
void merge(int arr[], int left, int mid, int right, int temp[], int *count) {
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
(*count)++;
}
while (i <= mid) {
temp[k++] = arr[i++];
(*count)++;
}
while (j <= right) {
temp[k++] = arr[j++];
(*count)++;
}
for (i = 0; i < k; i++) {
arr[left+i] = temp[i];
}
}
void merge_sort(int arr[], int left, int right, int temp[], int *count) {
if (left < right) {
int mid = (left + right) / 2;
merge_sort(arr, left, mid, temp, count);
merge_sort(arr, mid+1, right, temp, count);
merge(arr, left, mid, right, temp, count);
}
}
int main() {
int arr[MAX_SIZE];
int size = MAX_SIZE;
srand(time(NULL));
for (int i = 0; i < size; i++) {
arr[i] = rand() % 1000;
}
printf("Original array:\n");
print_array(arr, size);
int temp[MAX_SIZE];
int count;
// Direct insertion sort
count = direct_insertion_sort(arr, size);
printf("After direct insertion sort:\n");
print_array(arr, size);
printf("Comparison count: %d\n", count);
// Bubble sort
count = bubble_sort(arr, size);
printf("After bubble sort:\n");
print_array(arr, size);
printf("Comparison count: %d\n", count);
// Shell sort
count = shell_sort(arr, size);
printf("After shell sort:\n");
print_array(arr, size);
printf("Comparison count: %d\n", count);
// Quick sort
count = quick_sort(arr, 0, size-1);
printf("After quick sort:\n");
print_array(arr, size);
printf("Comparison count: %d\n", count);
// Merge sort
count = 0;
merge_sort(arr, 0, size-1, temp, &count);
printf("After merge sort:\n");
print_array(arr, size);
printf("Comparison count: %d\n", count);
return 0;
}
```
该程序先生成300个随机整数,然后使用5种排序算法进行排序,并统计比较次数。其中,直接插入排序、冒泡排序、希尔排序、快速排序和归并排序的算法实现分别为`direct_insertion_sort`、`bubble_sort`、`shell_sort`、`quick_sort`和`merge_sort`函数。最后,程序输出排序后的数组和比较次数。
阅读全文