c语言1.编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。要求:设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。
时间: 2023-08-03 20:03:31 浏览: 94
以下是一份C语言代码,实现了直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序,并且提供了一个菜单界面,用户可以选择要使用的排序算法:
```c
#include <stdio.h>
#include <stdlib.h>
// 直接插入排序
void insertion_sort(int arr[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
// 希尔排序
void shell_sort(int arr[], int n) {
int i, j, gap, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
}
}
// 冒泡排序
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 快速排序
void quick_sort(int arr[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right, temp = arr[left];
while (i < j) {
while (i < j && arr[j] >= temp) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < temp) {
i++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = temp;
quick_sort(arr, left, i - 1);
quick_sort(arr, i + 1, right);
}
// 选择排序
void selection_sort(int arr[], int n) {
int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
// 堆排序
void heap_sort(int arr[], int n) {
int i, temp;
// 建立堆
for (i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// 排序
for (i = n - 1; i > 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void heapify(int arr[], int n, int i) {
int largest = i, left = 2 * i + 1, right = 2 * i + 2, temp;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
int main() {
int n, i, choice;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
while (1) {
printf("\nSorting Menu:\n");
printf("1. Insertion Sort\n");
printf("2. Shell Sort\n");
printf("3. Bubble Sort\n");
printf("4. Quick Sort\n");
printf("5. Selection Sort\n");
printf("6. Heap Sort\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertion_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 2:
shell_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 3:
bubble_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 4:
quick_sort(arr, 0, n - 1);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 5:
selection_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 6:
heap_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 7:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
```
这份代码中,用户输入数组大小和数组元素后,程序进入一个无限循环的菜单界面,用户可以选择要使用哪种排序算法,或者退出界面。每次选择排序算法后,程序会调用对应的函数进行排序,并输出排序后的结果。
阅读全文