随机产生300个整数构成的序列,分别以直接插入、冒泡、希尔、快速、归并等排序算法排序,并统计各自的比较次数。用c语言实现
时间: 2023-08-18 08:09:18 浏览: 61
好的,我可以为您提供代码示例。以下是使用C语言实现的排序算法和比较次数统计函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 生成随机数序列
void generate_random_sequence(int sequence[], int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
sequence[i] = rand();
}
}
// 直接插入排序
int insertion_sort(int sequence[], int n) {
int comparisons = 0;
for (int i = 1; i < n; i++) {
int key = sequence[i];
int j = i - 1;
while (j >= 0 && sequence[j] > key) {
sequence[j + 1] = sequence[j];
j--;
comparisons++;
}
sequence[j + 1] = key;
}
return comparisons;
}
// 冒泡排序
int bubble_sort(int sequence[], int n) {
int comparisons = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (sequence[j] > sequence[j + 1]) {
int temp = sequence[j];
sequence[j] = sequence[j + 1];
sequence[j + 1] = temp;
comparisons++;
}
}
}
return comparisons;
}
// 希尔排序
int shell_sort(int sequence[], int n) {
int comparisons = 0;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = sequence[i];
int j;
for (j = i; j >= gap && sequence[j - gap] > temp; j -= gap) {
sequence[j] = sequence[j - gap];
comparisons++;
}
sequence[j] = temp;
}
}
return comparisons;
}
// 快速排序
int quick_sort(int sequence[], int left, int right) {
int comparisons = 0;
if (left < right) {
int pivot = sequence[right];
int i = left - 1;
for (int j = left; j <= right - 1; j++) {
if (sequence[j] <= pivot) {
i++;
int temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
comparisons++;
}
}
int temp = sequence[i + 1];
sequence[i + 1] = sequence[right];
sequence[right] = temp;
comparisons++;
int partition = i + 1;
comparisons += quick_sort(sequence, left, partition - 1);
comparisons += quick_sort(sequence, partition + 1, right);
}
return comparisons;
}
// 归并排序
void merge(int sequence[], int left, int mid, int right, int temp[], int* comparisons) {
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (sequence[i] <= sequence[j]) {
temp[k++] = sequence[i++];
} else {
temp[k++] = sequence[j++];
}
(*comparisons)++;
}
while (i <= mid) {
temp[k++] = sequence[i++];
(*comparisons)++;
}
while (j <= right) {
temp[k++] = sequence[j++];
(*comparisons)++;
}
for (int i = 0; i < k; i++) {
sequence[left + i] = temp[i];
}
}
int merge_sort(int sequence[], int left, int right, int temp[]) {
int comparisons = 0;
if (left < right) {
int mid = (left + right) / 2;
comparisons += merge_sort(sequence, left, mid, temp);
comparisons += merge_sort(sequence, mid + 1, right, temp);
merge(sequence, left, mid, right, temp, &comparisons);
}
return comparisons;
}
int main() {
int n = 300;
int sequence[n];
int temp[n];
generate_random_sequence(sequence, n);
printf("Original sequence:\n");
for (int i = 0; i < n; i++) {
printf("%d ", sequence[i]);
}
printf("\n");
int comparisons;
comparisons = insertion_sort(sequence, n);
printf("Insertion sort comparisons: %d\n", comparisons);
comparisons = bubble_sort(sequence, n);
printf("Bubble sort comparisons: %d\n", comparisons);
comparisons = shell_sort(sequence, n);
printf("Shell sort comparisons: %d\n", comparisons);
comparisons = quick_sort(sequence, 0, n - 1);
printf("Quick sort comparisons: %d\n", comparisons);
comparisons = merge_sort(sequence, 0, n - 1, temp);
printf("Merge sort comparisons: %d\n", comparisons);
printf("Sorted sequence:\n");
for (int i = 0; i < n; i++) {
printf("%d ", sequence[i]);
}
printf("\n");
return 0;
}
```
该程序通过调用 `generate_random_sequence` 函数生成随机数序列,然后使用五种不同的排序算法(直接插入、冒泡、希尔、快速、归并)对该序列进行排序,并统计各自的比较次数。最后打印排序后的序列。
请注意,该程序并没有直接回答您的问题,因为我是一个 AI 语言模型,我不能对您的问题进行具体回答,只能根据您的要求提供相关的代码示例。希望这能对您有所帮助。
阅读全文