十种排序算法:
- 选择排序
- 插入排序
- 冒泡排序
- 希尔排序
- 快速排序的三种实现方法
- 归并排序
- 堆排序(大顶堆)
- 计数排序
- 基数排序(待实现)
- 桶排序(待实现)
#include <bits/stdc++.h>
using namespace std;
void MergeSubQ(int a[], int n, int p, int middle, int r);
int Partition01(int a[], int p, int r);
int Partition02(int a[], int p, int r);
int* Partition03(int a[], int p, int r);
void InitializeHeap(int a[], int n, int i);//选择排序 O(n^2)
void SelectSort(int a[], int n){for(int i=0; i<n-1; i++){int mmin=a[i];int index=i;for(int j=i+1; j<n; j++){if(a[j]<mmin){mmin=a[j];index=j;}}int temp = a[i];a[i] = a[index];a[index] = temp;}
}//冒泡排序 O(n^2)
void BubbleSort(int a[], int n){for(int i=0; i<n-1; i++)for(int j=0; j<n-i-1; j++){if(a[j]>a[j+1]){int temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}
//插入排序 O(n^2)
void InsertSort(int a[], int n){for(int i=0; i<n; i++){int num = a[i];int index = i-1;while(index>-1 && num<a[index]){a[index+1] = a[index];index--;}a[index+1] = num;}
}//希尔排序 O(n^1.3)
void ShellSort(int a[], int n){for(int interval=n/2; interval>=1; interval/=2){for(int i=interval; i<n; i++){int num = a[i];int index = i-interval;while(index>-1 && num<a[index]){a[index+interval] = a[index];index-=interval;}a[index+interval] = num;}}
}//归并排序
void MergeSort(int a[], int n, int p, int r){if(p<r){int middle = p+((r-p)>>1);//递归求解子问题MergeSort(a, n, p, middle);MergeSort(a, n, middle+1, r);//合并MergeSubQ(a, n, p, middle, r);}
}
void MergeSubQ(int a[], int n, int p, int middle, int r){int help[n];//辅助数组for(int i=0; i<n; i++)help[i] = a[i];int left = p;//左指针int right = middle+1;//右指针int index=p;//当前要放入的位置while(left<=middle && right<=r){if(help[left]<=help[right]){ //左指针指向的元素较小,插入到a中a[index++] = help[left];left++;}if(help[left]>help[right]){ //右指针指向的元素较小,插入到a中a[index++] = help[right];right++;}}while(left<=middle){ //左部分还有剩余,直接插入a的末尾a[index++]=help[left];left++;}
}
//快速排序
void QuickSort(int a[], int p, int r){if(p<r){//找到主元应在的位置int* b = Partition03(a, p, r);//递归求解主元的左部分和右部分QuickSort(a, p, b[0]-1);QuickSort(a, b[1]+1, r);}
}
int Partition01(int a[], int p, int r){ //单向扫描法int point = a[p]; //主元int scan = p+1; //扫描指针,指向主元后int bigger = r; //指向末尾while(scan<bigger+1){if(a[scan]>point){ //扫描到了比主元大的元素int temp = a[scan]; //与bigger指向的后面的元素进行交换a[scan] = a[bigger];a[bigger] = temp;bigger--;}else //小于等于主元情况scan++;}a[p] = a[bigger]; //将主元放到正确位置a[bigger] = point;return bigger;
}
int Partition02(int a[], int p, int r){ //双指针int point = a[p];int left = p+1; //从左扫描int right = r; //从右扫描while(left<=right){while(left<=r && a[left]<point){left++;}while(right>=p+1 && a[right]>point){right--;}if(left<=right){int temp = a[left];a[left] = a[right];a[right] = temp;}}a[p] = a[right];a[right] = point;return right;
}
int* Partition03(int a[], int p, int r){ //重复元素过多时int point = a[p];int scan = p+1;int eq = p+1; //找重复int bigger = r;int flag=false;while(scan<=bigger){if(a[scan]<point){if(flag){int temp = a[scan];a[scan] = a[eq];a[eq] = temp;eq++;}scan++;}else if(a[scan]==point){ //相当时,将eq指向第一个相等的元素if(!flag){eq = scan;flag=true;}scan++;}else{int temp = a[scan];a[scan] = a[bigger];a[bigger] = temp;bigger--;}}if(!flag)eq=bigger;a[p] = a[bigger]; //bigger最后指向的是主元的位置a[bigger] = point;int b[2] = {eq, bigger}; //返回两个值return b;
}
//堆排序
void HeapSort(int a[], int n){//初始化堆for(int x=n/2; x>=0; x--)InitializeHeap(a, n, x);//进行排序for(int x=n-1; x>=0; x--){int temp = a[0];a[0] = a[x];a[x] = temp;InitializeHeap(a, x, 0);}}
void InitializeHeap(int a[], int n, int i){int left = 2*i+1;int right = 2*i+2;int maxIndex = left;if(left>=n)return ;if(right>=n){maxIndex = left;}else{if(a[right]>a[left])maxIndex=right;}if(a[i]>=a[maxIndex])return ;int temp = a[i];a[i] = a[maxIndex];a[maxIndex] = temp;InitializeHeap(a, n, maxIndex);
}
//基数排序
void BaseSort(int a[], int n){}//计数排序
void CountSort(int a[], int n){int mmax = a[0];for(int i=1; i<n; i++){ //找出数组中的最大值if(a[i]>mmax)mmax = a[i];}int help[mmax+1];memset(help, 0, sizeof(help));for(int i=0; i<n; i++){ //计数help[a[i]]++;}int index = 0;for(int i=1; i<mmax+1; i++){ //根据数的个数排序while(help[i]>0){a[index++] = i;help[i]--;}}
}//桶排序int main(){int a[10];srand((int)time(0));for(int i=0; i<10; i++)a[i] = rand() % 100;SelectSort(a, 10);BubbleSort(a, 10);InsertSort(a, 10);ShellSort(a, 10);MergeSort(a, 10, 0, 9);QuickSort(a, 0, 9);HeapSort(a, 10);CountSort(a, 10);for(int i=0; i<10; i++)cout<<a[i]<<" ";return 0;
}