参考:
No.1 冒泡排序
冒泡排序无疑是最为出名的排序算法之一,从序列的一端开始往另一端冒泡(你可以从左往右冒泡,也可以从右往左冒泡,看心情),依次比较相邻的两个数的大小(到底是比大还是比小也看你心情,即大的数冒泡还是小的数冒泡)。
#include <iostream>
using namespace std;
//冒泡排序
void sort1(int arr[], int len) {
//外层循环表示冒泡次数
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
//冒泡排序优化
void sort2(int arr[], int len) {
//外层循环表示冒泡次数
for (int i = 0; i < len - 1; i++) {
bool isSort = true;
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
isSort = false;
}
}
if (isSort) {
break;
}
}
}
int main() {
int arr[] = { 7, 6, 5, 4, 3, 2, 1 };
for (int i = 0; i < 7; i++) {
cout << arr[i] << " ";
}
cout << endl;
sort1(arr, 7);
for (int i = 0; i < 7; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
No.2 选择排序
No.3 插入排序
No.4 希尔排序
No.5 归并排序
归并字面上的意思是合并,归并算法的核心思想是分治法,就是将一个数组一刀切两半,递归切,直到切成单个元素,然后重新组装合并,单个元素合并成小数组,两个小数组合并成大数组,直到最终合并完成,排序完毕。
归并排序就是利用递归将问题不断切分,即将数组不断切分,切到最小粒度,每个数组就是有序的了,然后再将有序的每个小数组合并
#include <iostream>
using ElemType = int;
using namespace std;
void Merge(ElemType A[], ElemType B[], int Lpos, int Rpos, int RightEnd) {
int LeftEnd = Rpos - 1;
int tmpPos = Lpos;
int NumElements = RightEnd - Lpos + 1;
for (int i = Lpos; i <= RightEnd; i++)
B[i] = A[i];
while (Lpos <= LeftEnd && Rpos <= RightEnd) {
if (B[Lpos] <= B[Rpos])
A[tmpPos++] = B[Lpos++];
if (B[Lpos] > B[Rpos])
A[tmpPos++] = B[Rpos++];
}
while (Lpos <= LeftEnd)
A[tmpPos++] = B[Lpos++];
while (Rpos <= RightEnd)
A[tmpPos++] = B[Rpos++];
}
void Msort(ElemType A[], ElemType B[], int Left, int Right) {
int Center;
if (Left < Right) {
Center = (Left + Right) / 2;
Msort(A, B, Left, Center);
Msort(A, B, Center + 1, Right);
Merge(A, B, Left, Center + 1, Right);
}
}
void Mergesort(ElemType A[], int N) {
ElemType *TmpArray;
TmpArray = (ElemType *)malloc(N * sizeof(ElemType));
if (TmpArray != nullptr) {
Msort(A, TmpArray, 0, N - 1);
free(TmpArray);
}
}
int main() {
ElemType a[] = { 1, 13, 24, 26, 2, 15, 27, 38 };
Mergesort(a, 8);
for (int i = 0; i < 8; i++) {
cout << a[i] << " ";
}
cout << endl;
}
output:
1 13 24 26 2 15 27 38
请按任意键继续. . .
No.6 快速排序
快速排序的核心思想也是分治法,分而治之。它的实现方式是每次从序列中选出一个基准值,其他数依次和基准值做比较,比基准值大的放右边,比基准值小的放左边,然后再对左边和右边的两组数分别选出一个基准值,进行同样的比较移动,重复步骤,直到最后都变成单个元素,整个数组就成了有序的序列。
快速排序的关键之处在于切分,切分的同时要进行比较和移动
快排的思想就是(https://www.cnblogs.com/xiezhw3/p/3439031.html)
- 从数列中挑出一个元素,称为 "基准"(pivot),
- 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。
- 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序
那么这里需要考虑的就是如何进行分区,这个是最重要的,先来看看我的分区的实现:(这里的provit我就不用随机数来实现了,直接选第一个)
int partition(int array[], int begin, int end) {
int index = begin;
int proviet = array[begin];
swap(array[index], array[end]);//将provit换到最后
for(int i = begin; i != end; i++) {//将小于provit的元素放到左边,大的放到右边
if (array[i] < proviet)
swap(array[index++], array[i]);
}
swap(array[index], array[end]);
return index;
}
首先,将provit移到数组的最后一位,这样在数组分区的时候就不会影响到provit
#include <iostream>
using ElemType = int;
using namespace std;
//单边扫描
int Partition1(ElemType a[], int left, int right) {
//枢纽值选第一个
ElemType pivot = a[left];
swap(a[left], a[right]); //将pivot存于最后一个位置
//index左边的元素都属于小元素集合
int index = left;
for (int i = left; i < right; i++) {
if (a[i] < pivot) {
swap(a[index++], a[i]);
}
}
swap(a[index], a[right]);
return index;
}
void Qsort(ElemType A[], int left, int right) {
if (left < right) {
int i = Partition1(A, left, right);
Qsort(A, left, i - 1);
Qsort(A, i + 1, right);
}
}
int main() {
int a[] = { 1, 13, 24, 26, 24, 24, 2, 2, 2, 15, 27, 38 };
for (int i = 0; i < 12; i++) {
cout << a[i] << " ";
}
cout << endl;
Qsort(a, 0, 11);
for (int i = 0; i < 12; i++) {
cout << a[i] << " ";
}
cout << endl;
}
No.7 堆排序
参考:https://www.cnblogs.com/wanglei5205/p/8733524.html
下面程序也支持重复元素的排序
#include <iostream>
using ElemType = int;
using namespace std;
void adjust(ElemType A[], int size, int index);
//堆排序
void heapSort(ElemType A[], int size) {
//创建堆
for (int i = size / 2 - 1; i >= 0; i--) {
adjust(A, size, i);
}
//调整大根堆
for (int i = size - 1; i >= 1; i--) {
swap(A[0], A[i]);
adjust(A, i, 0);
}
}
//调整堆序:A为数组,size为数组尺寸,index为要调整的节点(即从最后一个不满足堆序性质的非叶节点)
void adjust(ElemType A[], int size, int index) {
int left = 2 * index + 1;
int right = 2 * index + 2;
int maxIndex = index;
if (left < size && A[left] > A[maxIndex])
maxIndex = left;
if (right < size && A[right] > A[maxIndex])
maxIndex = right;
if (maxIndex != index) {
swap(A[maxIndex], A[index]);
adjust(A, size, maxIndex);
}
}
int main() {
int a[] = { 8, 1, 14, 3, 21, 5, 7, 10 };
for (int i = 0; i < 8; i++) {
cout << a[i] << " ";
}
cout << endl;
heapSort(a, 8);
for (int i = 0; i < 8; i++) {
cout << a[i] << " ";
}
cout << endl;
}
output:
8 1 14 3 21 5 7 10
1 14 3 21 5 7 10 8
请按任意键继续. . .
No.8 计数排序
计数排序是一种非基于比较的排序算法,我们之前介绍的各种排序算法几乎都是基于元素之间的比较来进行排序的,计数排序的时间复杂度为 O(n + m ),m 指的是数据量,说的简单点,计数排序算法的时间复杂度约等于 O(n),快于任何比较型的排序算法。
#include <iostream>
using ElemType = int;
using namespace std;
//计数排序
void Sort(ElemType A[], int len) {
//找出数组中的最大值
int max = A[0];
for (int i = 1; i < len; i++) {
if (A[i] > max)
max = A[i];
}
ElemType *countArr = new ElemType[max + 1]();
for (int i = 0; i < len; i++) {
countArr[A[i]]++;
}
//排序
int index = 0;
for (int i = 0; i < max + 1; i++) {
while (countArr[i] > 0) {
A[index++] = i;
countArr[i]--;
}
}
}
int main() {
int a[] = { 8, 1, 14, 14, 3, 3, 3, 21, 5, 7, 10 };
for (int i = 0; i < 11; i++) {
cout << a[i] << " ";
}
cout << endl;
Sort(a, 11);
for (int i = 0; i < 11; i++) {
cout << a[i] << " ";
}
cout << endl;
}
output:
8 1 14 14 3 3 3 21 5 7 10
1 3 3 3 5 7 8 10 14 14 10
请按任意键继续. . .
No.9 桶排序
略
No.10 基数排序
略:参考:https://blog.csdn.net/bqw18744018044/article/details/81810190