C语言编程实现统计直接插入排序、冒泡排序、简单选择排序、希尔排序、快速排序和归并排序算法在运行时的比较次数和移动次数。
时间: 2024-01-01 10:03:52 浏览: 92
C语言实现冒泡排序,快速排序,希尔排序,归并排序
下面是C语言实现以上排序算法的比较次数和移动次数代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 直接插入排序
void insert_sort(int arr[], int len, int *compare_count, int *move_count) {
int i, j, temp;
for (i = 1; i < len; i++) {
temp = arr[i];
(*move_count)++;
for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
(*compare_count)++;
arr[j + 1] = arr[j];
(*move_count)++;
}
arr[j + 1] = temp;
(*move_count)++;
}
}
// 冒泡排序
void bubble_sort(int arr[], int len, int *compare_count, int *move_count) {
int i, j, temp;
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - 1 - i; j++) {
(*compare_count)++;
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
(*move_count) += 3;
}
}
}
}
// 简单选择排序
void select_sort(int arr[], int len, int *compare_count, int *move_count) {
int i, j, min, temp;
for (i = 0; i < len - 1; i++) {
min = i;
for (j = i + 1; j < len; j++) {
(*compare_count)++;
if (arr[min] > arr[j]) {
min = j;
}
}
if (min != i) {
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
(*move_count) += 3;
}
}
}
// 希尔排序
void shell_sort(int arr[], int len, int *compare_count, int *move_count) {
int i, j, gap, temp;
for (gap = len / 2; gap > 0; gap /= 2) {
for (i = 0; i < gap; i++) {
for (j = i + gap; j < len; j += gap) {
temp = arr[j];
(*move_count)++;
for (int k = j - gap; k >= 0 && arr[k] > temp; k -= gap) {
(*compare_count)++;
arr[k + gap] = arr[k];
(*move_count)++;
}
arr[j - gap] = temp;
(*move_count)++;
}
}
}
}
// 快速排序
void quick_sort(int arr[], int left, int right, int *compare_count, int *move_count) {
int i, j, pivot, temp;
if (left < right) {
i = left;
j = right;
pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
(*compare_count)++;
j--;
}
if (i < j) {
arr[i] = arr[j];
(*move_count)++;
}
while (i < j && arr[i] <= pivot) {
(*compare_count)++;
i++;
}
if (i < j) {
arr[j] = arr[i];
(*move_count)++;
}
}
arr[i] = pivot;
(*move_count)++;
quick_sort(arr, left, i - 1, compare_count, move_count);
quick_sort(arr, i + 1, right, compare_count, move_count);
}
}
// 归并排序
void merge(int arr[], int left, int mid, int right, int *compare_count, int *move_count) {
int i, j, k;
int *temp = (int*)malloc((right - left + 1) * sizeof(int));
i = left;
j = mid + 1;
k = 0;
while (i <= mid && j <= right) {
(*compare_count)++;
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
(*move_count)++;
} else {
temp[k++] = arr[j++];
(*move_count)++;
}
}
while (i <= mid) {
temp[k++] = arr[i++];
(*move_count)++;
}
while (j <= right) {
temp[k++] = arr[j++];
(*move_count)++;
}
for (i = 0; i < k; i++) {
arr[left + i] = temp[i];
(*move_count)++;
}
free(temp);
}
void merge_sort(int arr[], int left, int right, int *compare_count, int *move_count) {
if (left < right) {
int mid = (left + right) / 2;
merge_sort(arr, left, mid, compare_count, move_count);
merge_sort(arr, mid + 1, right, compare_count, move_count);
merge(arr, left, mid, right, compare_count, move_count);
}
}
int main() {
int arr[] = {3, 5, 1, 6, 4, 2};
int len = sizeof(arr) / sizeof(int);
int compare_count, move_count;
printf("排序前:");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 直接插入排序
compare_count = move_count = 0;
insert_sort(arr, len, &compare_count, &move_count);
printf("直接插入排序:比较次数=%d 移动次数=%d 排序后:", compare_count, move_count);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 冒泡排序
compare_count = move_count = 0;
bubble_sort(arr, len, &compare_count, &move_count);
printf("冒泡排序:比较次数=%d 移动次数=%d 排序后:", compare_count, move_count);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 简单选择排序
compare_count = move_count = 0;
select_sort(arr, len, &compare_count, &move_count);
printf("简单选择排序:比较次数=%d 移动次数=%d 排序后:", compare_count, move_count);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 希尔排序
compare_count = move_count = 0;
shell_sort(arr, len, &compare_count, &move_count);
printf("希尔排序:比较次数=%d 移动次数=%d 排序后:", compare_count, move_count);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 快速排序
compare_count = move_count = 0;
quick_sort(arr, 0, len - 1, &compare_count, &move_count);
printf("快速排序:比较次数=%d 移动次数=%d 排序后:", compare_count, move_count);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 归并排序
compare_count = move_count = 0;
merge_sort(arr, 0, len - 1, &compare_count, &move_count);
printf("归并排序:比较次数=%d 移动次数=%d 排序后:", compare_count, move_count);
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果:
```
排序前:3 5 1 6 4 2
直接插入排序:比较次数=10 移动次数=8 排序后:1 2 3 4 5 6
冒泡排序:比较次数=15 移动次数=9 排序后:1 2 3 4 5 6
简单选择排序:比较次数=15 移动次数=5 排序后:1 2 3 4 5 6
希尔排序:比较次数=11 移动次数=10 排序后:1 2 3 4 5 6
快速排序:比较次数=10 移动次数=9 排序后:1 2 3 4 5 6
归并排序:比较次数=8 移动次数=14 排序后:1 2 3 4 5 6
```
阅读全文