c语言完整代码编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。要求:设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。
时间: 2023-08-03 21:02:59 浏览: 93
很好,下面是完整的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
void printArray(int arr[], int size);
void swap(int *a, int *b);
void insertionSort(int arr[], int n);
void shellSort(int arr[], int n);
void bubbleSort(int arr[], int n);
void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void selectionSort(int arr[], int n);
void heapify(int arr[], int n, int i);
void heapSort(int arr[], int n);
int main() {
int arr[100], n, i, choice;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
while (1) {
printf("\nSorting 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("7. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertionSort(arr, n);
printf("\nSorted array using Insertion Sort: ");
printArray(arr, n);
break;
case 2:
shellSort(arr, n);
printf("\nSorted array using Shell Sort: ");
printArray(arr, n);
break;
case 3:
bubbleSort(arr, n);
printf("\nSorted array using Bubble Sort: ");
printArray(arr, n);
break;
case 4:
quickSort(arr, 0, n - 1);
printf("\nSorted array using Quick Sort: ");
printArray(arr, n);
break;
case 5:
selectionSort(arr, n);
printf("\nSorted array using Selection Sort: ");
printArray(arr, n);
break;
case 6:
heapSort(arr, n);
printf("\nSorted array using Heap Sort: ");
printArray(arr, n);
break;
case 7:
exit(0);
default:
printf("\nInvalid choice\n");
break;
}
}
return 0;
}
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void shellSort(int arr[], int n) {
int gap, i, j, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
}
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 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);
}
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
int j;
for (j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void selectionSort(int arr[], int n) {
int i, j, min_idx;
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;
}
swap(&arr[min_idx], &arr[i]);
}
}
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
```
这个程序会要求用户输入数组和排序算法的选择,然后根据选择的算法进行排序。在每个排序算法完成后,程序会打印排序后的数组。
阅读全文