随机产生300个整数构成的序列,分别以直接插入、冒泡、希尔、快速、归并等排序算法排序,并统计各自的比较次数。用c语言实现
时间: 2023-08-06 21:08:23 浏览: 76
以下是用C语言实现的代码,其中包含直接插入排序、冒泡排序、希尔排序、快速排序和归并排序算法以及计数比较次数的函数。
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LENGTH 300 // 序列长度
int insertion_sort(int arr[], int n); // 直接插入排序
int bubble_sort(int arr[], int n); // 冒泡排序
int shell_sort(int arr[], int n); // 希尔排序
int quick_sort(int arr[], int low, int high); // 快速排序
int merge_sort(int arr[], int left, int right); // 归并排序
void merge(int arr[], int left, int mid, int right); // 归并排序中的合并函数
void generate_random(int arr[], int n); // 随机生成序列
void print_array(int arr[], int n); // 打印序列
int compare_count = 0; // 记录比较次数
int main() {
int arr[LENGTH];
generate_random(arr, LENGTH); // 随机生成序列
printf("Original sequence:\n");
print_array(arr, LENGTH);
// 直接插入排序
compare_count = insertion_sort(arr, LENGTH);
printf("\nInsertion sort result:\n");
print_array(arr, LENGTH);
printf("Number of comparisons: %d\n", compare_count);
// 冒泡排序
generate_random(arr, LENGTH); // 重新生成随机序列
compare_count = bubble_sort(arr, LENGTH);
printf("\nBubble sort result:\n");
print_array(arr, LENGTH);
printf("Number of comparisons: %d\n", compare_count);
// 希尔排序
generate_random(arr, LENGTH); // 重新生成随机序列
compare_count = shell_sort(arr, LENGTH);
printf("\nShell sort result:\n");
print_array(arr, LENGTH);
printf("Number of comparisons: %d\n", compare_count);
// 快速排序
generate_random(arr, LENGTH); // 重新生成随机序列
compare_count = quick_sort(arr, 0, LENGTH - 1);
printf("\nQuick sort result:\n");
print_array(arr, LENGTH);
printf("Number of comparisons: %d\n", compare_count);
// 归并排序
generate_random(arr, LENGTH); // 重新生成随机序列
compare_count = merge_sort(arr, 0, LENGTH - 1);
printf("\nMerge sort result:\n");
print_array(arr, LENGTH);
printf("Number of comparisons: %d\n", compare_count);
return 0;
}
/**
* 直接插入排序
*/
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;
}
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++;
}
}
}
return count;
}
/**
* 希尔排序
*/
int shell_sort(int arr[], int n) {
int i, j, gap, temp, count = 0;
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];
count++;
}
arr[j] = temp;
}
}
return count;
}
/**
* 快速排序
*/
int quick_sort(int arr[], int low, int high) {
int i, j, pivot, temp, count = 0;
if (low < high) {
pivot = arr[low];
i = low;
j = high;
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
count++;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < pivot) {
i++;
count++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = pivot;
quick_sort(arr, low, i - 1);
quick_sort(arr, i + 1, high);
}
return count;
}
/**
* 归并排序
*/
int merge_sort(int arr[], int left, int right) {
int count = 0;
if (left < right) {
int mid = (left + right) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
return count;
}
/**
* 归并排序中的合并函数
*/
void merge(int arr[], int left, int mid, int right) {
int temp[LENGTH];
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
compare_count++;
} else {
temp[k++] = arr[j++];
compare_count++;
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = 0; i < k; i++) {
arr[left + i] = temp[i];
}
}
/**
* 随机生成序列
*/
void generate_random(int arr[], int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
}
}
/**
* 打印序列
*/
void print_array(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
```
运行程序后,会输出每个算法排序后的序列以及比较次数。
阅读全文