几种常见的排序算法实现

时间:2023-02-17 09:44:10

1. 插入排序

//插入排序
#include <iostream>
using namespace std;

//每次遍历将第i个数插入到前i-1个数中(前i-1个数已排好序)
void insertSort(int a[], int n)
{
for (int i=1;i<n;++i)
{
int tmp = a[i];
int j;
for (j=i-1;j>=0 && a[j] > tmp;--j)
{
a[j+1] = a[j];
}
if(j+1 != i)
a[j+1] = tmp;
}
}

void printArray(int a[], int n)
{
for (int i=0;i<n;++i)
{
cout<<a[i]<<'\t';
}
}

int main()
{
int a[] = {49, 38, 65, 97, 76, 13, 27, 49};
insertSort(a, 8);
printArray(a, 8);
cout<<endl;
return 0;
}

2. 希尔排序

//希尔排序
//思想:当待排序列基本有序时,直接插入排序的效率很高。故先将整个待排记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行一次直接插入排序
#include <iostream>
using namespace std;

//每次遍历将第i个数插入到前i-1个数中(前i-1个数已排好序)
void shellInsert(int a[], int n, int dk)
{
//对数组a作一趟希尔插入排序。本算法和一趟直接插入排序相比,作了以下修改:
// 1. 前后记录位置的增量是dk,而不是1;
for (int i=dk;i<n;++i)
{
//现在将a[i]插入有序增量子表
int tmp = a[i];
int j;
for (j=i-dk;j>=0 && a[j] > tmp;j=j-dk)
{
a[j+dk] = a[j];
}
//将tmp放到j+dk的位置
if (j+dk != i) a[j+dk] = tmp;
}
}

void printArray(int a[], int n)
{
for (int i=0;i<n;++i)
{
cout<<a[i]<<' ';
}
}

void shellSort(int a[], int n, int dlta[], int dn)
{
for (int i=0;i<dn;++i)
{
shellInsert(a, n, dlta[i]);
cout<<endl;
cout<<"经过增量为"<<dlta[i]<<"的排序后:";
printArray(a, 10);
}
}

int main()
{
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
cout<<"原始数据:";
printArray(a, 10);
int d[] = {5, 3, 1};
shellSort(a, 10, d, 3);
cout<<endl;
return 0;
}

运行结果:
几种常见的排序算法实现

3. 快速排序

//快速排序
//思想:通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
#include <iostream>
using namespace std;

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 quickSort(int a[], int low, int high)
{
if (low < high)
{
int pivot = partition(a, low, high); //将a[low...high]一分为二
quickSort(a, low, pivot-1);
quickSort(a, pivot+1, high);
}
}
void printArray(int a[], int n)
{
for (int i=0;i<n;++i)
{
cout<<a[i]<<' ';
}
}

int main()
{
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
cout<<"原始数据:";
printArray(a, 10);
cout<<endl;
quickSort(a, 0, 9);
cout<<"排序后:";
printArray(a, 10);
cout<<endl;
return 0;
}

4. 选择排序

//选择排序
//思想:通过n-i次关键字间的比较,从n-i+1个记录中选出关键字最小的的记录,并和第i个记录交换
#include <iostream>
using namespace std;

void selectSort(int a[], int n)
{
for (int i=0;i<n-1;++i)
{
int minInd = i;
for (int j=i+1;j<n;++j)
{
if (a[j] < a[minInd])
{
minInd = j;
}
}
if (minInd != i)
{
int tmp = a[i];
a[i] = a[minInd];
a[minInd] = tmp;
}
}
}

void printArray(int a[], int n)
{
for (int i=0;i<n;++i)
{
cout<<a[i]<<' ';
}
}

int main()
{
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
cout<<"原始数据:";
printArray(a, 10);
cout<<endl;
selectSort(a, 10);
cout<<"排序后:";
printArray(a, 10);
cout<<endl;
return 0;
}

5 . 归并排序

//归并排序
//思想:假设初始序列含有n个记录,则可看成是n个有序子序列,每个子序列的长度为1,然后两两归并,......,如此重复
// 直至得到一个长度为n的有序序列位置,这种排序方法称为2-路归并排序。
#include <iostream>
using namespace std;

//归并[startIndex, midIndex]和[midIndex+1, endIndex]的区间
void Merge(int sourceArr[],int tempArr[], int startIndex, int midIndex, int endIndex)
{
int i = startIndex;
int j = midIndex+1;
int k = startIndex;
for (;i<=midIndex && j<=endIndex;)
{
if (sourceArr[i] <= sourceArr[j])
{
tempArr[k++] = sourceArr[i++];
}
else
{
tempArr[k++] = sourceArr[j++];
}
}
while(i<=midIndex)
tempArr[k++] = sourceArr[i++];
while(j<=endIndex)
tempArr[k++] = sourceArr[j++];
for (int i=startIndex;i<=endIndex;++i)
{
sourceArr[i] = tempArr[i];
}
}

//内部使用递归
void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex)
{
if (startIndex < endIndex)
{
int midIndex = (startIndex+endIndex)/2;
MergeSort(sourceArr, tempArr, startIndex, midIndex);
MergeSort(sourceArr, tempArr, midIndex+1, endIndex);
Merge(sourceArr, tempArr, startIndex, midIndex, endIndex);
}
}

void printArray(int a[], int n)
{
for (int i=0;i<n;++i)
{
cout<<a[i]<<' ';
}
}

int main()
{
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4, 60};
int b[11];
cout<<"原始数据:";
printArray(a, 11);
cout<<endl;
MergeSort(a, b, 0, 10);
cout<<"排序后:";
printArray(a, 11);
cout<<endl;
return 0;
}

6. 堆排序
堆排序的算法以前写过一遍,这里就不再写了,详见LeetCode第23题之Merge k Sorted Lists

稳定性:基数排序是稳定的内排方法,所有时间复杂度为O(n^2)的简单排序算法也是稳定的,然而,快速排序、堆排序和希尔排序等时间性能较好的排序方法都是不稳定的。一般来说,排序过程中的“比较”是在“相邻的两个记录关键字”间进行的排序方法是稳定的。