排序算法是最为常见的算法,今天就来整理一下这些常见的排序算法:
1. 插入排序算法:
包括:直接插入排序(稳定的,时间复杂度O(n^2)) 折半插入排序(稳定的, 时间复杂度O(n^2),但是减少了比较次数)
2-路插入排序 (稳定的,时间复杂度O(n^2)) 希尔排序(不稳定的,时间复杂度不确定)
2. 快速排序:
包括:冒泡排序(最常见的排序方法,稳定的,时间复杂度O(n^2) ) 快速排序(不稳定, 时间复杂度O(nlogn) )
3. 选择排序:
包括:简答选择排序(稳定的,时间复杂度O(n^2)) 堆排序(不稳定, 时间复杂度O(nlogn) )
4. 归并排序
包括: 2-路归并排序(不稳定, 时间复杂度O(nlogn) )
C++实现代码:
#include <iostream>
#include <iomanip>
using namespace std;
#define MAXSIZE 20
typedef int KeyType;
struct SqList{
int key[MAXSIZE+1];
int length;
};
打印列表
void print(const SqList &L){
for(int i = 1; i <= L.length; i++)
cout << std::left << setw(4) << setfill(' ') << L.key[i];
cout << endl;
}
插入排序
void InsertSort(SqList &L){ //插入排序
for(int i = 2; i <= L.length; i++){
L.key[0] = L.key[i];
int j;
for (j = i-1; L.key[0] < L.key[j]; j--)
L.key[j+1] = L.key[j];
L.key[j+1] = L.key[0];
}
print(L);
}
void BInsertSort(SqList& L){ //二分法的插入排序
for (int i = 2; i <= L.length; i++)
{
L.key[0] = L.key[i];
int j, mid, low = 1, high = L.length;
while( high >= low){
mid = (low + high)/2;
if( L.key[mid] < L.key[0] )
low = mid + 1;
else
high = mid - 1;
}
for(j = i-1; j >= high+1; j--)
L.key[j+1] = L.key[j];
L.key[high+1] = L.key[0];
}
print(L);
}
void TwoInsertSort(SqList &L){ //两路插入排序
SqList dL = {{0},10};
int first = L.length, final = L.length;
dL.key[final] = L.key[1];
for(int i = 2; i <= L.length; i++){
dL.key[0] = L.key[i];
if ( dL.key[0] < dL.key[final] )
{
if( first == final ){
first = 1;
dL.key[first] = dL.key[0];
}else{
int j, mid, low = 1, high = first;
while( high >= low){
mid = (low + high)/2;
if( dL.key[mid] < dL.key[0] )
low = mid + 1;
else
high = mid - 1;
}
for(j = first; j >= high+1; j--)
dL.key[j+1] = dL.key[j];
dL.key[high+1] = dL.key[0];
first++;
}
}else{
int j, mid, low = final, high = dL.length;
while( high >= low){
mid = (low + high)/2;
if( dL.key[mid] < dL.key[0] )
low = mid + 1;
else
high = mid - 1;
}
for( j = final; j <= high; j++)
dL.key[j-1] = dL.key[j];
dL.key[high] = dL.key[0];
final--;
}
print(dL);
}
}
void ShellPartition(SqList &L, const int dl){ //希尔排序
for (int i = 1+dl; i <= L.length; i++)
{
L.key[0] = L.key[i];
int j;
for ( j = i-dl; j >=0 && (L.key[0] < L.key[j]); j -= dl)
L.key[j+dl] = L.key[j];
L.key[j+dl] = L.key[0];
}
}
void ShellSort(SqList &L, int list[], int len){
for (int i = 0; i < len; i++)
{
ShellPartition(L,list[i]);
print(L);
}
}
//快速排序///
void BubbleSort(SqList &L){ //冒泡排序
for (int i = 1; i < L.length; i++)
{
for (int j = L.length; j > i; j--)
{
if( L.key[j] < L.key[j-1] ){
L.key[0] = L.key[j];
L.key[j] = L.key[j-1];
L.key[j-1] = L.key[0];
}
}
print(L);
}
}
int QuickPartition(SqList &L, int low, int high){ //快速排序
L.key[0] = L.key[low];
while( low < high ){
while(low < high && L.key[0] <= L.key[high] )
high--;
L.key[low] = L.key[high];
while(low < high && L.key[low] <= L.key[0] )
low++;
L.key[high] = L.key[low];
}
L.key[low] = L.key[0];
return low;
}
void QuickSort(SqList &L, int low, int high){
int pivotkey;
if ( low < high )
{
pivotkey = QuickPartition(L, low, high);
QuickSort(L, low, pivotkey-1);
QuickSort(L, pivotkey+1, high);
print(L);
}
}
//选择排序///
void SelectSort(SqList &L){ //简单选择排序
for (int i = 1; i < L.length; i++)
{
int temp;
L.key[0] = i;
for (int j = i+1; j <= L.length; j++)
{
if (L.key[j] < L.key[L.key[0]])
L.key[0] = j;
}
if ( L.key[0] != i)
{
temp = L.key[i];
L.key[i] = L.key[L.key[0]];
L.key[L.key[0]] = temp;
}
}
print(L);
}
void HeapAdjust(SqList &L, int beg, int end){ //堆排序
L.key[0] = L.key[beg];
for (int i = beg*2; i <= end; i = i*2)
{
if ( i < end && L.key[i] < L.key[i+1])
i++;
if ( L.key[i] > L.key[0] ){
L.key[beg] = L.key[i];
beg = i;
}
}
L.key[beg] = L.key[0];
}
void HeapSort(SqList &L){
for ( int i = L.length/2; i >= 1; i--)
HeapAdjust(L, i, L.length);
print(L);
for ( int i = L.length; i > 1; i--)
{
L.key[0] = L.key[1];
L.key[1] = L.key[i];
L.key[i] = L.key[0];
HeapAdjust(L, 1, i-1);
}
print(L);
}
归并排序//
void Merge(SqList &L, int i, int m, int n){
SqList dL = {{0}, n-i+1};
int j,k,beg=i;
for(j = m+1, k = 1; i <= m && j <= n; k++){
if ( L.key[j] < L.key[i] )
dL.key[k] = L.key[j++];
else
dL.key[k] = L.key[i++];
}
while(i <= m)
dL.key[k++] = L.key[i++];
while(j <= n)
dL.key[k++] = L.key[j++];
for (i = beg, k = 1; i <= n; )
L.key[i++] = dL.key[k++];
}
void MergeSort(SqList &L, int s, int t){
int m;
if(s < t){
m = (s + t)/2;
MergeSort(L,s,m);
MergeSort(L,m+1,t);
Merge(L,s,m,t);
}
print(L);
}
void OrdiMergeSort(SqList &L){ //非递归归并排序
int beforLen;
int afterLen = 1;
for (beforLen = 1; afterLen <= L.length; beforLen = afterLen )
{
afterLen = beforLen << 1;
int i = 1;
for (; i+afterLen <= L.length; i += afterLen)
Merge(L, i, i+beforLen-1, i+afterLen-1);
if (i+beforLen <= L.length)
Merge(L, i, i+beforLen-1, L.length);
}
print(L);
}
int main(){
//SqList list = {{0,49,38,65,97,76,13,27,49,55,4,71},11};
SqList list = {{0,49,38,65,97,76,13,27,49},8};
int dl[3] = {5,3,1};
print(list);
//TwoInsertSort(list);
//ShellSort(list,dl,3);
//BubbleSort(list);
//QuickSort(list,1,list.length);
//SelectSort(list);
OrdiMergeSort(list);
return 0;
}