# C++常见排序算法

## 1. 直接插入排序

#include <iostream>
#include <vector>
using namespace std;

int main() {
int n;
cin >> n;
vector<int> arr(n + 1, 0);
for (int i = 1; i <= n; ++i)
cin >> arr[i];
for (int i = 2; i <= n; ++i)
if (arr[i] < arr[i - 1]) {
arr[0] = arr[i];
int j;
for (j = i - 1; arr[0] < arr[j]; j--)
arr[j + 1] = arr[j];
arr[j + 1] = arr[0];
}
for (int i = 1; i <= n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}


## 2. 二分插入排序

#include <iostream>
#include <vector>
using namespace std;

int main() {
int n;
cin >> n;
vector<int> arr(n + 1, 0);
for (int i = 1; i <= n; ++i)
cin >> arr[i];
for (int i = 2; i <= n; ++i) {
arr[0] = arr[i];
int low = 1, high = i - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[0] < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
int j;
for (j = i - 1; j > high; j--)
arr[j + 1] = arr[j];
arr[j + 1] = arr[0];
}
for (int i = 1; i <= n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}
}


## 3. 希尔排序

#include <iostream>
#include <vector>
using namespace std;

void insertI(vector<int>& arr, int gap, int i) {   //插入排序
int inserted = arr[i];
int j;
for (j = i - gap; j >= 0 && inserted < arr[j]; j -= gap)
arr[j + gap] = arr[j];
arr[j + gap] = inserted;
}

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)
insertI(arr, gap, i);
}

int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
shellSort(arr);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}


## 4. 冒泡排序

#include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& arr, int low, int high) {    //找分割点
int pivot = arr[low];
while (low < high) {
while (low < high && arr[high] >= pivot)
high--;
arr[low] = arr[high];
while (low < high && arr[low] <= pivot)
low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}

void quickSort(vector<int>& arr, int low, int high) {
if (low < high) {
int pivotloc = partition(arr, low, high);
quickSort(arr, low, pivotloc - 1);
quickSort(arr, pivotloc + 1, high);
}
}

int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}


## 6. 简单选择排序

#include <iostream>
#include <vector>
using namespace std;

int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
for (int i = 0; i < n - 1; ++i) {    //找到最小数的索引
int index = i;
for (int j = i + 1; j < n; ++j)
if (arr[j] < arr[index])
index = j;
if (index != i) {
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}


## 7. 堆排序（小根堆）

#include <iostream>
#include <vector>
using namespace std;

void minHeapDown(vector<int>& arr, int start, int end) {
int cur = start;
int left = 2 * cur + 1;
int temp = arr[cur];
for (; left <= end; cur = left, left = 2 * left + 1) {
if (left < end && arr[left] > arr[left + 1])
left++;
if (temp <= arr[left])
break;
else {
arr[cur] = arr[left];
arr[left] = temp;
}
}
}

void heapSortIncr(vector<int>& arr, int n) {
int temp = 0;
for (int i = n / 2 - 1; i >= 0; i--)
minHeapDown(arr, i, n - 1);
for (int i = n - 1; i > 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
minHeapDown(arr, 0, i - 1);
}
}

int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
heapSortIncr(arr, n);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}


## 8. 堆排序（大根堆）

#include <iostream>
#include <vector>
using namespace std;
void maxHeapDown(vector<int>& arr, int start, int end) {
int cur = start;
int left = 2 * cur + 1;
int temp = arr[cur];
for (; left <= end; cur = left, left = 2 * left + 1) {
if (left < end && arr[left] < arr[left + 1])
left++;
if (temp >= arr[left])
break;
else {
arr[cur] = arr[left];
arr[left] = temp;
}
}
}
void heapSortIncr(vector<int>& arr, int n) {
int temp = 0;
for (int i = n / 2 - 1; i >= 0; i--)
maxHeapDown(arr, i, n - 1);
for (int i = n - 1; i > 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
maxHeapDown(arr, 0, i - 1);
}
}
int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
heapSortIncr(arr, n);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}



## 9. 归并排序

#include <iostream>
#include <vector>
using namespace std;

void Merge(vector<int>& arr, int low, int mid, int high) {   //归并
int i = low, j = mid + 1, k = 0;   //将数组分成两部分
vector<int> temp(high - low + 1);  //开辟一个临时数组
while (i <= mid && j <= high) {    //
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= mid)
temp[k++] = arr[i++];
while (j <= high)
temp[k++] = arr[j++];
k = 0;
while(low<=high)
arr[low++] = temp[k++];
}

void mergeSort(vector<int>& arr, int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
Merge(arr, low, mid, high);
}
}

int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
mergeSort(arr, 0, n-1);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}



## 10.基数排序

#include <iostream>
#include <vector>
using namespace std;

void radixSort(vector<int>& arr, int n) {
int max = arr[0];
for (int i = 1; i < n; ++i)
max = max > arr[i] ? max : arr[i];  //找最大值
for (int i = 1; max / i > 0; i *= 10) {  //按个位入桶->按十位入桶->...
vector<vector<int>> buckets(10, vector<int>(n, -1));  //10个桶
for (int j = 0; j < n; ++j) {
int num = arr[j] / i % 10;
buckets[num][j] = arr[j];
}
int index = 0;
for (int j = 0; j < 10; ++j)  //按个位排序取出->按十位排序取出->...
for (int k = 0; k < n; ++k)
if (buckets[j][k] != -1)
arr[index++] = buckets[j][k];
}
}

int main() {
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; ++i)
cin >> arr[i];
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}


06-01 3817
05-09 647
09-15 543
05-28
05-05 6426
08-19