随机产生300个整数构成的序列,分别以直接插入、冒泡、希尔、快速、归并等排序算法排序,并统计各自的比较次数。用c语言实现
时间: 2023-08-18 14:08:41 浏览: 94
好的,下面是使用C语言实现的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 产生随机数序列
void generate_random_sequence(int a[], int n) {
srand(time(NULL)); // 设置随机数种子
for (int i = 0; i < n; i++) {
a[i] = rand() % 1000; // 生成0-999之间的随机整数
}
}
// 直接插入排序
int insertion_sort(int a[], int n) {
int count = 0; // 比较次数
for (int i = 1; i < n; i++) {
int temp = a[i];
int j = i - 1;
while (j >= 0 && a[j] > temp) {
a[j + 1] = a[j];
j--;
count++;
}
a[j + 1] = temp;
}
return count;
}
// 冒泡排序
int bubble_sort(int a[], int n) {
int count = 0; // 比较次数
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
count++;
}
}
}
return count;
}
// 希尔排序
int shell_sort(int a[], int n) {
int count = 0; // 比较次数
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = a[i];
int j = i - gap;
while (j >= 0 && a[j] > temp) {
a[j + gap] = a[j];
j -= gap;
count++;
}
a[j + gap] = temp;
}
}
return count;
}
// 快速排序
int quick_sort(int a[], int left, int right) {
int count = 0; // 比较次数
if (left < right) {
int i = left, j = right, pivot = a[left];
while (i < j) {
while (i < j && a[j] >= pivot) {
j--;
count++;
}
if (i < j) {
a[i++] = a[j];
}
while (i < j && a[i] < pivot) {
i++;
count++;
}
if (i < j) {
a[j--] = a[i];
}
}
a[i] = pivot;
count += quick_sort(a, left, i - 1);
count += quick_sort(a, i + 1, right);
}
return count;
}
// 归并排序
int merge(int a[], int left, int mid, int right) {
int count = 0; // 比较次数
int i = left, j = mid + 1, k = 0;
int *temp = (int*)malloc(sizeof(int) * (right - left + 1));
while (i <= mid && j <= right) {
if (a[i] <= a[j]) {
temp[k++] = a[i++];
} else {
temp[k++] = a[j++];
}
count++;
}
while (i <= mid) {
temp[k++] = a[i++];
}
while (j <= right) {
temp[k++] = a[j++];
}
for (int p = 0; p < k; p++) {
a[left + p] = temp[p];
}
free(temp);
return count;
}
int merge_sort(int a[], int left, int right) {
int count = 0; // 比较次数
if (left < right) {
int mid = (left + right) / 2;
count += merge_sort(a, left, mid);
count += merge_sort(a, mid + 1, right);
count += merge(a, left, mid, right);
}
return count;
}
int main() {
int a[300];
int count_insertion, count_bubble, count_shell, count_quick, count_merge;
generate_random_sequence(a, 300);
int b[300];
for (int i = 0; i < 300; i++) {
b[i] = a[i];
}
count_insertion = insertion_sort(b, 300);
printf("直接插入排序比较次数:%d\n", count_insertion);
for (int i = 0; i < 300; i++) {
b[i] = a[i];
}
count_bubble = bubble_sort(b, 300);
printf("冒泡排序比较次数:%d\n", count_bubble);
for (int i = 0; i < 300; i++) {
b[i] = a[i];
}
count_shell = shell_sort(b, 300);
printf("希尔排序比较次数:%d\n", count_shell);
for (int i = 0; i < 300; i++) {
b[i] = a[i];
}
count_quick = quick_sort(b, 0, 299);
printf("快速排序比较次数:%d\n", count_quick);
for (int i = 0; i < 300; i++) {
b[i] = a[i];
}
count_merge = merge_sort(b, 0, 299);
printf("归并排序比较次数:%d\n", count_merge);
return 0;
}
```
代码中,我们首先定义了一个函数 `generate_random_sequence`,用于产生随机数序列。接着,我们分别实现了直接插入排序、冒泡排序、希尔排序、快速排序、归并排序等排序算法,并统计了各自的比较次数。最后,在 `main` 函数中,我们生成了一个随机数序列,并分别调用上述排序算法对其进行排序,并输出各自的比较次数。
需要注意的是,由于快速排序的时间复杂度具有不稳定性,所以其比较次数可能会出现波动。而归并排序的比较次数与序列的初始状态无关,因此其比较次数应该是最稳定的。
阅读全文