编程实现直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。要求:设计菜单界面、每个排序算法通过选菜单实现,同时要求界面有退出菜单。数据结构C++写
时间: 2024-06-13 07:09:51 浏览: 111
各种排序算法C++的实现(冒泡,选择,插入,快速,归并,堆)
5星 · 资源好评率100%
以下是C++实现六种排序算法的代码,包括直接插入排序、希尔排序、冒泡排序、快速排序、选择排序和堆排序。同时,代码中还包括了一个简单的菜单界面,可以通过选菜单实现各种排序算法,同时也可以退出菜单。
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// 生成随机数组
void generateRandomArray(int arr[], int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand();
}
}
// 打印数组
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// 直接插入排序
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int temp = arr[i];
int 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) {
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i - gap;
while (j >= 0 && arr[j] > temp) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = temp;
}
}
}
// 冒泡排序
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 快速排序
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// 选择排序
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
// 堆排序
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
// 菜单界面
void menu() {
int n = 20000;
int arr[n];
int choice;
while (true) {
cout << "请选择排序算法:" << endl;
cout << "1. 直接插入排序" << endl;
cout << "2. 希尔排序" << endl;
cout << "3. 冒泡排序" << endl;
cout << "4. 快速排序" << endl;
cout << "5. 选择排序" << endl;
cout << "6. 堆排序" << endl;
cout << "7. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
generateRandomArray(arr, n);
insertionSort(arr, n);
cout << "直接插入排序结果:" << endl;
printArray(arr, n);
break;
case 2:
generateRandomArray(arr, n);
shellSort(arr, n);
cout << "希尔排序结果:" << endl;
printArray(arr, n);
break;
case 3:
generateRandomArray(arr, n);
bubbleSort(arr, n);
cout << "冒泡排序结果:" << endl;
printArray(arr, n);
break;
case 4:
generateRandomArray(arr, n);
quickSort(arr, 0, n - 1);
cout << "快速排序结果:" << endl;
printArray(arr, n);
break;
case 5:
generateRandomArray(arr, n);
selectionSort(arr, n);
cout << "选择排序结果:" << endl;
printArray(arr, n);
break;
case 6:
generateRandomArray(arr, n);
heapSort(arr, n);
cout << "堆排序结果:" << endl;
printArray(arr, n);
break;
case 7:
return;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
}
int main() {
menu();
return 0;
}
```
阅读全文