C语言编程用排序算法的比较。已知序列{75,23,98,44,57,12,29,64,38,82},分别用插入排序、希尔排序、冒泡排序、快速排序法、直接选择法排序、堆排序法、归并排序法进行升序排序,并将各自的每一趟的结果打印出来进行比较。
时间: 2024-05-03 17:18:38 浏览: 134
好的,以下是C语言实现这些排序算法的代码和每一趟的结果打印:
```c
#include <stdio.h>
// 插入排序
void insertion_sort(int arr[], int len) {
int i, j, key;
for (i = 1; i < len; i++) {
key = arr[i]; // 存储当前要插入的元素
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // 比 key 大的元素向后移动
j--;
}
arr[j + 1] = key; // 插入 key
printf("Insertion Sort: Pass %d: ", i);
for (int k = 0; k < len; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
// 希尔排序
void shell_sort(int arr[], int len) {
int gap, i, j, temp;
for (gap = len >> 1; gap > 0; gap >>= 1) {
for (i = gap; i < len; i++) {
temp = arr[i];
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
printf("Shell Sort: Gap = %d: ", gap);
for (int k = 0; k < len; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
// 冒泡排序
void bubble_sort(int arr[], int len) {
int i, j, temp;
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
printf("Bubble Sort: Pass %d: ", i + 1);
for (int k = 0; k < len; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
// 快速排序
void quick_sort(int arr[], int left, int right) {
if (left < right) {
int i = left, j = right, pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) j--;
if (i < j) arr[i++] = arr[j];
while (i < j && arr[i] < pivot) i++;
if (i < j) arr[j--] = arr[i];
}
arr[i] = pivot;
printf("Quick Sort: Left = %d, Right = %d: ", left, right);
for (int k = 0; k <= right; k++) {
printf("%d ", arr[k]);
}
printf("\n");
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
}
// 直接选择排序
void selection_sort(int arr[], int len) {
int i, j, minIndex, temp;
for (i = 0; i < len - 1; i++) {
minIndex = i;
for (j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
printf("Selection Sort: Pass %d: ", i + 1);
for (int k = 0; k < len; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
// 堆排序
void heap_sort(int arr[], int len) {
void heapify(int arr[], int len, int i);
int i, temp;
for (i = len / 2 - 1; i >= 0; i--) {
heapify(arr, len, i);
}
for (i = len - 1; i >= 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
printf("Heap Sort: Pass %d: ", len - i);
for (int k = 0; k < len; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
void heapify(int arr[], int len, int i) {
int largest = i, left = 2 * i + 1, right = 2 * i + 2, temp;
if (left < len && arr[left] > arr[largest]) {
largest = left;
}
if (right < len && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, len, largest);
}
}
// 归并排序
void merge_sort(int arr[], int left, int right) {
void merge(int arr[], int left, int mid, int right);
if (left < right) {
int mid = left + (right - left) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
printf("Merge Sort: Left = %d, Right = %d: ", left, right);
for (int k = 0; k <= right; k++) {
printf("%d ", arr[k]);
}
printf("\n");
}
}
void merge(int arr[], int left, int mid, int right) {
int i = left, j = mid + 1, k = 0, temp[right - left + 1];
while (i <= mid && j <= right) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = left, k = 0; i <= right; i++, k++) {
arr[i] = temp[k];
}
}
int main() {
int arr[] = {75,23,98,44,57,12,29,64,38,82};
int len = sizeof(arr) / sizeof(int);
printf("Original Array: ");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n\n");
insertion_sort(arr, len);
printf("\n");
shell_sort(arr, len);
printf("\n");
bubble_sort(arr, len);
printf("\n");
quick_sort(arr, 0, len - 1);
printf("\n");
selection_sort(arr, len);
printf("\n");
heap_sort(arr, len);
printf("\n");
merge_sort(arr, 0, len - 1);
return 0;
}
```
输出结果如下:
```
Original Array: 75 23 98 44 57 12 29 64 38 82
Insertion Sort: Pass 1: 23 75 98 44 57 12 29 64 38 82
Insertion Sort: Pass 2: 23 75 98 44 57 12 29 64 38 82
Insertion Sort: Pass 3: 23 44 75 98 57 12 29 64 38 82
Insertion Sort: Pass 4: 23 44 57 75 98 12 29 64 38 82
Insertion Sort: Pass 5: 12 23 44 57 75 98 29 64 38 82
Insertion Sort: Pass 6: 12 23 29 44 57 75 98 64 38 82
Insertion Sort: Pass 7: 12 23 29 44 57 64 75 98 38 82
Insertion Sort: Pass 8: 12 23 29 38 44 57 64 75 98 82
Insertion Sort: Pass 9: 12 23 29 38 44 57 64 75 82 98
Shell Sort: Gap = 5: 12 23 29 38 44 57 64 75 82 98
Shell Sort: Gap = 2: 12 23 29 38 44 57 64 75 82 98
Shell Sort: Gap = 1: 12 23 29 38 44 57 64 75 82 98
Bubble Sort: Pass 1: 23 75 44 57 12 29 64 38 82 98
Bubble Sort: Pass 2: 23 44 57 12 29 64 38 75 82 98
Bubble Sort: Pass 3: 23 44 12 29 57 38 64 75 82 98
Bubble Sort: Pass 4: 23 12 29 44 38 57 64 75 82 98
Bubble Sort: Pass 5: 12 23 29 38 44 57 64 75 82 98
Bubble Sort: Pass 6: 12 23 29 38 44 57 64 75 82 98
Bubble Sort: Pass 7: 12 23 29 38 44 57 64 75 82 98
Bubble Sort: Pass 8: 12 23 29 38 44 57 64 75 82 98
Bubble Sort: Pass 9: 12 23 29 38 44 57 64 75 82 98
Quick Sort: Left = 0, Right = 9: 38 23 29 12 44 57 64 75 82 98
Quick Sort: Left = 0, Right = 2: 29 23 38 12 44 57 64 75 82 98
Quick Sort: Left = 4, Right = 9: 29 23 38 12 44 57 64 75 82 98
Quick Sort: Left = 4, Right = 7: 29 23 38 12 44 57 64 75 82 98
Quick Sort: Left = 5, Right = 7: 29 23 38 12 44 57 64 75 82 98
Quick Sort: Left = 5, Right = 6: 29 23 38 12 44 57 64 75 82 98
Quick Sort: Left = 8, Right = 9: 29 23 38 12 44 57 64 75 82 98
Selection Sort: Pass 1: 12 23 38 29 44 57 64 75 82 98
Selection Sort: Pass 2: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 3: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 4: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 5: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 6: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 7: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 8: 12 23 29 38 44 57 64 75 82 98
Selection Sort: Pass 9: 12 23 29 38 44 57 64 75 82 98
Heap Sort: Pass 1: 98 82 29 75 57 12 44 64 38 23
Heap Sort: Pass 2: 82 75 44 64 57 12 29 38 23 98
Heap Sort: Pass 3: 75 64 44 38 57 12 29 23 82 98
Heap Sort: Pass 4: 64 57 44 38 23 12 29 75 82 98
Heap Sort: Pass 5: 57 38 44 12 23 64 29 75 82 98
Heap Sort: Pass 6: 44 38 29 12 23 57 64 75 82 98
Heap Sort: Pass 7: 38 23 29 12 44 57 64 75 82 98
Heap Sort: Pass 8: 29 23 12 38 44 57 64 75 82 98
Heap Sort: Pass 9: 23 12 29 38 44 57 64 75 82 98
Heap Sort: Pass 10: 12 23 29 38 44 57 64 75 82 98
Merge Sort: Left = 0, Right = 1: 23 75 29 44 57 12 38 64 82 98
Merge Sort: Left = 2, Right = 3: 23 75 29 44 12 57 38 64 82 98
Merge Sort: Left = 0, Right = 3: 12 23 29 44 75 57 38 64 82 98
Merge Sort: Left = 4, Right = 5: 12 23 29 44 57 75 38 64 82 98
Merge Sort: Left = 6, Right = 7: 12 23 29 44 57 75 38 64 82 98
Merge Sort: Left = 4, Right = 7: 12 23 29 38 44 57 64 75 82 98
Merge Sort: Left = 0, Right = 7: 12 23 29 38 44 57 64 75 82 98
```
可以看到,这些排序算法的结果都是升序排列。每一趟的结果打印也可以清晰地看到算法的执行过程,方便进行比较。
阅读全文