用c语言编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。要求:设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。
时间: 2023-08-12 07:08:52 浏览: 101
六种内部排序算法比较:直接插入排序、希尔排序、冒泡排序、快速排序、选择排序、堆排序。
很高兴为您提供代码实现,以下是完整的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, tmp, gap;
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;
}
}
}
}
// 快速排序
void QuickSort(int arr[], int left, int right) {
int i, j, pivot, tmp;
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) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;
QuickSort(arr, left, j - 1);
QuickSort(arr, j + 1, right);
}
}
// 选择排序
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[min_idx];
arr[min_idx] = arr[i];
arr[i] = tmp;
}
}
// 堆排序
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 Heapify(int arr[], int n, int i) {
int largest = i, l = 2 * i + 1, r = 2 * i + 2, 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);
}
}
int main() {
int n, i, choice;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
while (1) {
printf("\n");
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("7. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
InsertionSort(arr, n);
printf("\nSorted array using Insertion Sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
break;
case 2:
ShellSort(arr, n);
printf("\nSorted array using Shell Sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
break;
case 3:
BubbleSort(arr, n);
printf("\nSorted array using Bubble Sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
break;
case 4:
QuickSort(arr, 0, n - 1);
printf("\nSorted array using Quick Sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
break;
case 5:
SelectionSort(arr, n);
printf("\nSorted array using Selection Sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
break;
case 6:
HeapSort(arr, n);
printf("\nSorted array using Heap Sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
break;
case 7:
printf("\nExiting the program\n");
exit(0);
default:
printf("\nInvalid choice\n");
}
}
return 0;
}
```
运行程序后,用户可以根据菜单选择所需的排序算法,程序会输出排序后的结果。当用户选择退出菜单时,程序将停止执行。
注意:为了演示方便,上述代码中的数组大小是在运行时动态分配的,并且假设用户输入的数据都是整数类型。如果需要使用其他类型的数据,需要相应地修改代码。
阅读全文