c语言1.编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。要求:设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。
时间: 2023-08-03 19:03:31 浏览: 69
以下是一个简单的实现,包含了直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序的代码以及一个菜单界面。用户可以通过输入菜单选项来选择需要使用的排序算法,也可以选择退出程序。
```c
#include <stdio.h>
#include <stdlib.h>
void insert_sort(int arr[], int n);
void shell_sort(int arr[], int n);
void bubble_sort(int arr[], int n);
void quick_sort(int arr[], int left, int right);
void select_sort(int arr[], int n);
void heap_sort(int arr[], int n);
int main() {
int arr[100], n, i, choice;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
do {
printf("\n1. Insertion Sort");
printf("\n2. Shell Sort");
printf("\n3. Bubble Sort");
printf("\n4. Quick Sort");
printf("\n5. Selection Sort");
printf("\n6. Heap Sort");
printf("\n7. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
insert_sort(arr, n);
break;
case 2:
shell_sort(arr, n);
break;
case 3:
bubble_sort(arr, n);
break;
case 4:
quick_sort(arr, 0, n - 1);
break;
case 5:
select_sort(arr, n);
break;
case 6:
heap_sort(arr, n);
break;
case 7:
printf("\nExiting program...");
exit(0);
default:
printf("\nInvalid choice!");
}
printf("\nSorted array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
} while(choice != 7);
return 0;
}
void insert_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;
}
printf("\nInsertion sort complete!");
}
void shell_sort(int arr[], int n) {
int i, j, temp, gap;
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;
}
}
printf("\nShell sort complete!");
}
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;
}
}
}
printf("\nBubble sort complete!");
}
void quick_sort(int arr[], int left, int right) {
int pivot, i, j, temp;
if(left < right) {
pivot = left;
i = left;
j = right;
while(i < j) {
while(arr[i] <= arr[pivot] && i < right) {
i++;
}
while(arr[j] > arr[pivot]) {
j--;
}
if(i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;
quick_sort(arr, left, j - 1);
quick_sort(arr, j + 1, right);
}
printf("\nQuick sort complete!");
}
void select_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;
}
printf("\nSelection sort complete!");
}
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
int temp;
if(l < n && arr[l] > arr[largest]) {
largest = l;
}
if(r < n && arr[r] > arr[largest]) {
largest = r;
}
if(largest != i) {
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
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);
}
printf("\nHeap sort complete!");
}
```
注意,以上代码只是一个简单的实现,可能不是最优的。还有很多其他的排序算法可以使用,也可以根据具体的需求进行修改和优化。
阅读全文