用c++编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。要求:设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。
时间: 2023-08-11 15:07:42 浏览: 107
很高兴为您提供这个问题的解答。下面是一个使用C++实现各种排序算法的示例程序,其中包括直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。程序还包括一个菜单界面,用户可以在其中选择要使用的算法。
```c++
#include <iostream>
using namespace std;
//直接插入排序
void InsertSort(int a[], int n)
{
int i, j, k;
for (i = 1; i < n; i++)
{
k = a[i];
j = i - 1;
while (j >= 0 && k < a[j])
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = k;
}
}
//希尔排序
void ShellSort(int a[], int n)
{
int i, j, k, gap;
for (gap = n / 2; gap > 0; gap /= 2)
{
for (i = gap; i < n; i++)
{
k = a[i];
j = i - gap;
while (j >= 0 && k < a[j])
{
a[j + gap] = a[j];
j -= gap;
}
a[j + gap] = k;
}
}
}
//冒泡排序
void BubbleSort(int a[], int n)
{
int i, j, t;
for (i = 0; i < n - 1; i++)
{
for (j = n - 1; j > i; j--)
{
if (a[j] < a[j - 1])
{
t = a[j];
a[j] = a[j - 1];
a[j - 1] = t;
}
}
}
}
//快速排序
int Partition(int a[], int low, int high)
{
int pivotkey = a[low];
while (low < high)
{
while (low < high && a[high] >= pivotkey) high--;
a[low] = a[high];
while (low < high && a[low] <= pivotkey) low++;
a[high] = a[low];
}
a[low] = pivotkey;
return low;
}
void QSort(int a[], int low, int high)
{
int pivotloc;
if (low < high)
{
pivotloc = Partition(a, low, high);
QSort(a, low, pivotloc - 1);
QSort(a, pivotloc + 1, high);
}
}
void QuickSort(int a[], int n)
{
QSort(a, 0, n - 1);
}
//选择排序
void SelectSort(int a[], int n)
{
int i, j, min, t;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
if (min != i)
{
t = a[min];
a[min] = a[i];
a[i] = t;
}
}
}
//堆排序
void HeapAdjust(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2 * i + 1;
while (j < n)
{
if (j + 1 < n && a[j + 1] > a[j])
{
j++;
}
if (a[j] <= temp)
{
break;
}
a[(j - 1) / 2] = a[j];
j = 2 * j + 1;
}
a[(j - 1) / 2] = temp;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n / 2 - 1; i >= 0; i--)
{
HeapAdjust(a, i, n);
}
for (i = n - 1; i > 0; i--)
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
HeapAdjust(a, 0, i);
}
}
//显示菜单
void ShowMenu()
{
cout << "请选择要使用的排序算法:" << endl;
cout << "1. 直接插入排序" << endl;
cout << "2. 希尔排序" << endl;
cout << "3. 冒泡排序" << endl;
cout << "4. 快速排序" << endl;
cout << "5. 选择排序" << endl;
cout << "6. 堆排序" << endl;
cout << "0. 退出" << endl;
}
int main()
{
int a[100], n, choice;
while (true)
{
ShowMenu();
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入要排序的数据个数:";
cin >> n;
cout << "请输入要排序的数据:";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
InsertSort(a, n);
cout << "排序后的结果为:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
break;
case 2:
cout << "请输入要排序的数据个数:";
cin >> n;
cout << "请输入要排序的数据:";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
ShellSort(a, n);
cout << "排序后的结果为:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
break;
case 3:
cout << "请输入要排序的数据个数:";
cin >> n;
cout << "请输入要排序的数据:";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
BubbleSort(a, n);
cout << "排序后的结果为:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
break;
case 4:
cout << "请输入要排序的数据个数:";
cin >> n;
cout << "请输入要排序的数据:";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
QuickSort(a, n);
cout << "排序后的结果为:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
break;
case 5:
cout << "请输入要排序的数据个数:";
cin >> n;
cout << "请输入要排序的数据:";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
SelectSort(a, n);
cout << "排序后的结果为:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
break;
case 6:
cout << "请输入要排序的数据个数:";
cin >> n;
cout << "请输入要排序的数据:";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
HeapSort(a, n);
cout << "排序后的结果为:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
break;
case 0:
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
}
```
在上面的程序中,我们首先定义了6个排序算法的函数,然后定义了一个显示菜单的函数。在主函数中,我们使用一个while循环来让用户不断选择要使用的算法,直到用户选择退出为止。对于每个算法,我们首先读入要排序的数据,然后调用相应的排序函数进行排序,最后输出排序后的结果。
希望这个程序能够帮助您理解各种排序算法的实现。
阅读全文