#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<cstdio>
#include<string.h>
using namespace std;
int temp;//哨兵
int len=10000;//对十万个数排序,有重复
bool isPrint=false;//是否输出数组
vector<int> myArray;
double startT,endT;//计算时间
void print(vector<int> myVector)
{
for(int i=0;i<len;i++)
{
cout<<myVector[i]<<" ";
if(i%10==0&&i!=0)
cout<<endl;
}
cout<<endl;
}
//初始化函数排序数范围在0~99999,有重复,总数100000
void init(){
srand((unsigned)time(NULL));
for(int i=0;i<len;i++)
{
myArray.push_back(rand()%len);
}
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
//快排子函数
//快速排序选取第一个数为标兵,返回最后在数组中位置
int Partition(vector<int> &tempArray,int low,int high){
int temp=tempArray[low];
while(low<high)
{
while(high>low&&tempArray[high]>=temp)
{
high--;
}
swap(tempArray[high],tempArray[low]);
while(low<high&&tempArray[low]<=temp)
{
low++;
}
swap(tempArray[high],tempArray[low]);
}
tempArray[low]=temp;
return low;
}
//快速排序
void QuickSort(vector<int> &tempArray,int low,int high){
if(low<high)
{
int pivot=Partition(tempArray,low,high);
QuickSort(tempArray,low,pivot);
QuickSort(tempArray,pivot+1,high);
}
}
//计数排序
vector<int> CountingSort(){
int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
int max=myArray[pos]+1;
vector<int> count(max);//计数数组
vector<int> result(len);//排序结束数组
for(int i=0;i<len;i++)
{
count[myArray[i]]++;
}
for(int i=1;i<=max;i++)
{
count[i]=count[i]+count[i-1];
}
for(int i=0;i<len;i++)
{
count[myArray[i]]--;
result[count[myArray[i]]]=myArray[i];
}
return result;
}
//桶排序
//最快情况是取每个桶大小为1,但这个意义不大,几乎等同于计数排序了,因此我们取桶大小为100,桶内部用sort(一般为改进版快排)
int sizeOfBucket=10000;
vector<int> BucketSort(){
int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
int max=myArray[pos]+1;
vector<vector<int>> bucket(max/sizeOfBucket+1);
for(int i=0;i<len;i++)
{
bucket[myArray[i]/sizeOfBucket].push_back(myArray[i]);
}
for(int i=0;i<max/sizeOfBucket+1;i++)
{
sort(bucket[i].begin(),bucket[i].end());
}
vector<int> result;
for(int i=0;i<max/sizeOfBucket+1;i++)
{
for(int j=0;j<bucket[i].size();j++)
{
result.push_back(bucket[i][j]);
}
}
return result;
}
//基数排序子函数(用桶排序)
void ExpSort(vector<int>&tempArray,int exp){
int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
int max=myArray[pos]+1;
vector<vector<int>> bucket(max/exp+1);
for(int i=0;i<len;i++)
{
bucket[myArray[i]/exp].push_back(myArray[i]);
}
for(int i=0;i<max/exp+1;i++)
{
sort(bucket[i].begin(),bucket[i].end());
}
vector<int> result;
for(int i=0;i<max/exp+1;i++)
{
for(int j=0;j<bucket[i].size();j++)
{
result.push_back(bucket[i][j]);
}
}
tempArray=result;
}
//基数排序 分别对个位,十位等排序
vector<int> RadixSort(vector<int> tempArray){
int pos = (int)(max_element(myArray.begin(),myArray.end()) - myArray.begin());
int max=myArray[pos]+1;
for(int exp=1;max/exp>0;exp*=10)
{
ExpSort(tempArray,exp);
}
return tempArray;
}
int main() {
init();
vector<int> tempArray(myArray);
if(isPrint) {
cout<<"初始数组:"<<endl;
print(myArray);
}
cout<<"sort排序: ";
startT=clock();
vector<int> sortArray(tempArray);
sort(sortArray.begin(),sortArray.end());
endT=clock();
cout<<"时间 "<<endT-startT<<endl;
if(isPrint)
print(tempArray);
cout<<"计数排序: ";
startT=clock();
tempArray=CountingSort();
endT=clock();
cout<<"时间 "<<endT-startT<<endl;
if(isPrint)
print(tempArray);
cout<<"桶排序:";
startT=clock();
tempArray=BucketSort();
endT=clock();
cout<<"时间 "<<endT-startT<<endl;
if(isPrint) {
print(tempArray);
}
cout<<"基数排序:";
startT=clock();
tempArray=RadixSort(tempArray);
endT=clock();
cout<<"时间 "<<endT-startT<<endl;
if(isPrint) {
print(tempArray);
}
cout<<"快速排序:";
startT=clock();
QuickSort(tempArray,0,len-1);
endT=clock();
cout<<"时间 "<<endT-startT<<endl;
if(isPrint)
print(tempArray);
return 0;
}