yongc语言编写一段代码,随机产生300个整数构成的序列,分别以直接插入、冒泡、希尔、快速、归并等排序算法排序,并统计各自的比较次数。
时间: 2023-07-10 19:05:43 浏览: 112
以下是使用yongc语言编写的代码,它随机生成300个整数,然后分别使用直接插入、冒泡、希尔、快速和归并排序算法进行排序,并统计比较次数。
```c
#include <stdlib.h>
#include <stdio.h>
// 定义比较函数指针类型
typedef int (*cmp_func)(int, int);
// 交换两个元素的值
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// 直接插入排序
int direct_insertion_sort(int arr[], int n, cmp_func cmp) {
int count = 0;
for (int i = 1; i < n; i++) {
int temp = arr[i];
int j = i - 1;
while (j >= 0 && cmp(arr[j], temp) > 0) {
arr[j + 1] = arr[j];
j--;
count++;
}
arr[j + 1] = temp;
count++;
}
return count;
}
// 冒泡排序
int bubble_sort(int arr[], int n, cmp_func cmp) {
int count = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (cmp(arr[j], arr[j + 1]) > 0) {
swap(&arr[j], &arr[j + 1]);
count++;
}
count++;
}
}
return count;
}
// 希尔排序
int shell_sort(int arr[], int n, cmp_func cmp) {
int count = 0;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && cmp(arr[j - gap], temp) > 0) {
arr[j] = arr[j - gap];
j -= gap;
count++;
}
arr[j] = temp;
count++;
}
}
return count;
}
// 快速排序
int quick_sort(int arr[], int left, int right, cmp_func cmp) {
int count = 0;
if (left < right) {
int pivot = arr[left];
int i = left, j = right;
while (i < j) {
while (i < j && cmp(pivot, arr[j]) <= 0) {
j--;
count++;
}
arr[i] = arr[j];
while (i < j && cmp(pivot, arr[i]) >= 0) {
i++;
count++;
}
arr[j] = arr[i];
count++;
}
arr[i] = pivot;
count++;
count += quick_sort(arr, left, i - 1, cmp);
count += quick_sort(arr, i + 1, right, cmp);
}
return count;
}
// 归并排序
void merge(int arr[], int left, int mid, int right, int temp[], cmp_func cmp, int *count) {
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (cmp(arr[i], arr[j]) <= 0) {
temp[k++] = arr[i++];
(*count)++;
} 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];
(*count)++;
}
}
int merge_sort(int arr[], int left, int right, int temp[], cmp_func cmp) {
int count = 0;
if (left < right) {
int mid = (left + right) / 2;
count += merge_sort(arr, left, mid, temp, cmp);
count += merge_sort(arr, mid + 1, right, temp, cmp);
merge(arr, left, mid, right, temp, cmp, &count);
}
return count;
}
// 比较函数,用于升序排序
int cmp(int a, int b) {
return a - b;
}
int main() {
int arr[300];
int temp[300];
int count;
// 随机生成300个整数
for (int i = 0; i < 300; i++) {
arr[i] = rand() % 100;
}
// 直接插入排序
count = direct_insertion_sort(arr, 300, cmp);
printf("直接插入排序:比较次数:%d\n", count);
// 冒泡排序
count = bubble_sort(arr, 300, cmp);
printf("冒泡排序:比较次数:%d\n", count);
// 希尔排序
count = shell_sort(arr, 300, cmp);
printf("希尔排序:比较次数:%d\n", count);
// 快速排序
count = quick_sort(arr, 0, 299, cmp);
printf("快速排序:比较次数:%d\n", count);
// 归并排序
count = merge_sort(arr, 0, 299, temp, cmp);
printf("归并排序:比较次数:%d\n", count);
return 0;
}
```
阅读全文