用c++实现排序综合采用插入排序、希尔排序、起泡排序、快速排序、选择排序、堆排序、归并排序)方法实现
时间: 2023-08-01 12:07:09 浏览: 128
常用c++排序算法(包括插入排序、冒泡排序、选择排序、快速排序、归并排序、希尔排序等)
好的,以下是用C++实现七种排序算法的代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 插入排序
void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// 希尔排序
void shellSort(vector<int>& arr) {
int n = arr.size();
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
}
// 冒泡排序
void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
// 快速排序
int partition(vector<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++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void quickSort(vector<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(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
swap(arr[i], arr[min_idx]);
}
}
// 堆排序
void heapify(vector<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) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(vector<int>& arr) {
int n = arr.size();
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
// 归并排序
void merge(vector<int>& arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
vector<int> L(n1);
vector<int> R(n2);
for (int i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
int i = 0;
int j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(vector<int>& arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
vector<int> arr = {5, 2, 4, 6, 1, 3};
insertionSort(arr);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
arr = {5, 2, 4, 6, 1, 3};
shellSort(arr);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
arr = {5, 2, 4, 6, 1, 3};
bubbleSort(arr);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
arr = {5, 2, 4, 6, 1, 3};
quickSort(arr, 0, arr.size() - 1);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
arr = {5, 2, 4, 6, 1, 3};
selectionSort(arr);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
arr = {5, 2, 4, 6, 1, 3};
heapSort(arr);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
arr = {5, 2, 4, 6, 1, 3};
mergeSort(arr, 0, arr.size() - 1);
for (int i : arr) {
cout << i << " ";
}
cout << endl;
return 0;
}
```
以上是七种排序算法的代码实现,你可以自行运行并测试。
阅读全文