c语言编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序,设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。
时间: 2023-08-18 19:07:50 浏览: 98
好的,下面是 C 语言编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序,设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 直接插入排序
void insertionSort(int arr[], int n) {
int i, j, tmp;
for (i = 1; i < n; i++) {
tmp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > tmp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = tmp;
}
}
// 希尔排序
void shellSort(int arr[], int n) {
int i, j, gap, tmp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
tmp = arr[i];
for (j = i - gap; j >= 0 && arr[j] > tmp; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = tmp;
}
}
}
// 冒泡排序
void bubbleSort(int arr[], int n) {
int i, j, tmp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
// 快速排序
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
int j, tmp;
for (j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = tmp;
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// 选择排序
void selectionSort(int arr[], int n) {
int i, j, min_idx, tmp;
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;
}
}
tmp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = tmp;
}
}
// 堆排序
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
int tmp;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
tmp = arr[i];
arr[i] = arr[largest];
arr[largest] = tmp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
int i, tmp;
for (i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (i = n - 1; i >= 0; i--) {
tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
heapify(arr, i, 0);
}
}
// 打印数组
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n, choice, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
while (1) {
printf("Sorting Algorithms\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("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
break;
case 2:
shellSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
break;
case 3:
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
break;
case 4:
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
break;
case 5:
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
break;
case 6:
heapSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
break;
case 0:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
运行该程序,将会显示菜单,您可以选择要使用的排序算法,或退出程序。
阅读全文