用C语言编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序,要有菜单。
时间: 2023-08-01 13:14:11 浏览: 89
直接插入、折半插入、冒泡、快速、简单选择等排序方法 用c语言实现
好的,以下是用C语言实现六种排序算法并提供菜单的代码:
```c
#include <stdio.h>
#include <stdlib.h>
void directInsertionSort(int arr[], int n);
void shellSort(int arr[], int n);
void bubbleSort(int arr[], int n);
void quickSort(int arr[], int left, int right);
void selectionSort(int arr[], int n);
void heapSort(int arr[], int n);
int main()
{
int arr[100], n, i, choice;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
while(1)
{
printf("\nSorting Algorithms:\n");
printf("1. Direct 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 0:
exit(0);
case 1:
directInsertionSort(arr, n);
break;
case 2:
shellSort(arr, n);
break;
case 3:
bubbleSort(arr, n);
break;
case 4:
quickSort(arr, 0, n-1);
break;
case 5:
selectionSort(arr, n);
break;
case 6:
heapSort(arr, n);
break;
default:
printf("Invalid choice\n");
break;
}
printf("Sorted array:\n");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");
}
return 0;
}
void directInsertionSort(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 i, j, gap, 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 left, int right)
{
int i, j, pivot, 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;
quickSort(arr, left, j-1);
quickSort(arr, j+1, right);
}
}
void selectionSort(int arr[], int n)
{
int i, j, min_index, temp;
for(i=0; i<n-1; i++)
{
min_index = i;
for(j=i+1; j<n; j++)
{
if(arr[j]<arr[min_index])
min_index = j;
}
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
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 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);
}
}
```
这段代码实现了六种排序算法:直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。在主函数中,首先输入需要排序的数组,然后提供一个菜单,根据用户的选择进行相应的排序。每次排序后,都会输出排序后的数组。用户输入0则退出程序。
阅读全文