用C语言编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序,要有菜单
时间: 2023-08-01 20:14:11 浏览: 66
好的,以下是完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 直接插入排序
void insertSort(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 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, 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 quickSort(int arr[], int low, int high) {
int i, j, pivot, temp;
if (low < high) {
pivot = low;
i = low;
j = high;
while (i < j) {
while (arr[i] <= arr[pivot] && i <= high) {
i++;
}
while (arr[j] > arr[pivot] && j >= low) {
j--;
}
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[j];
arr[j] = arr[pivot];
arr[pivot] = temp;
quickSort(arr, low, j - 1);
quickSort(arr, j + 1, high);
}
}
// 选择排序
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
// 堆排序
void heapSort(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;
int left = 2 * i + 1;
int right = 2 * i + 2;
int 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);
}
}
// 打印数组
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int option, n, i, *arr;
printf("Enter the number of elements: ");
scanf("%d", &n);
arr = (int*)malloc(n * sizeof(int));
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
do {
printf("\n1. 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", &option);
switch (option) {
case 0:
printf("Exiting...\n");
break;
case 1:
insertSort(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;
default:
printf("Invalid choice!\n");
break;
}
} while (option != 0);
free(arr);
return 0;
}
```
使用方法:
1. 编译运行代码;
2. 输入待排序数组的元素个数和数组元素;
3. 选择排序算法进行排序,直到退出。
注意:该程序仅支持整数排序。
阅读全文