为了应付考试,重新温习一下各种排序算法hhhhh
希尔排序和基排就暂时忽略了。。。。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;
const int MAXN=10000;
clock_t start,stop;
double duration;
void GetArray(int ary[]) {
srand((int)time(0));
for (long long i=0;i<MAXN;i++){
ary[i]=rand()%MAXN;
}
}
void QuickSort(int a[],long long left,long long right){
long long lower=left+1;
long long upper=right;
swap(a[left],a[(left+right)/2]);
int temp=a[left];
while (lower<=upper){
while (a[lower]<temp){
lower++;
}
while (a[upper]>temp){
upper--;
}
if(lower<upper){
swap(a[lower++],a[upper--]);
}else{
lower++;
}
}
swap(a[upper],a[left]);
if(left<upper-1){
QuickSort(a,left,upper-1);
}
if(upper+1<left){
QuickSort(a,upper+1,right);
}
return ;
}
void Merge(int sourceArr[],int tempArr[],int startIndex,int midIndex,int endIndex){
int i=startIndex;
int j=midIndex+1;
int k=startIndex;
while(i!=midIndex+1 && j!=endIndex+1){
if(sourceArr[i]>=sourceArr[j]){
tempArr[k++]=sourceArr[j++];
}else{
tempArr[k++]=sourceArr[i++];
}
}
while(i != midIndex+1){
tempArr[k++]=sourceArr[i++];
}
while(j != endIndex+1){
tempArr[k++]=sourceArr[j++];
}
for(i=startIndex;i<=endIndex;i++){
sourceArr[i]=tempArr[i];
}
}
void MergeSort(int sourceArr[],int tempArr[],int startIndex,int endIndex)
{
int midIndex;
if(startIndex < endIndex){
midIndex=(startIndex + endIndex) / 2;
MergeSort(sourceArr,tempArr,startIndex,midIndex);
MergeSort(sourceArr,tempArr,midIndex+1,endIndex);
Merge(sourceArr,tempArr,startIndex,midIndex,endIndex);
}
return ;
}
void HeapAdjust(int array[],int i,int nLength){
int nChild;
int nTemp;
for(;2*i+1<nLength;i=nChild){
nChild=2*i+1;
if(nChild<nLength-1 && array[nChild+1]>array[nChild]){
++nChild;
}
if(array[i]<array[nChild]){
nTemp = array[i];
array[i] = array[nChild];
array[nChild] = nTemp;
}else{
break;
}
}
}
void HeapSort(int array[],int length){
for(int i=length/2-1;i>=0;--i){
HeapAdjust(array,i,length);
}
for(int i=length-1;i>0;--i){
array[i]=array[0]^array[i];
array[0]=array[0]^array[i];
array[i]=array[0]^array[i];
HeapAdjust(array,0,i);
}
return ;
}
void BubbleSort(int a[],long long len){
for (long long i=0;i<len-1;i++){
for (long long j=0;j<len-1-i;j++){
if(a[j]>a[j+1]){
swap(a[j+1],a[j]);
}
}
}
return ;
}
void SelectSort(int a[],long long len){
for (int i=0;i<len;i++){
int index=i;
int j;
for (j=i+1;j<len;j++){
if(a[j]<a[index]){
index=j;
}
}
swap(a[index],a[i]);
}
return ;
}
void InsertSort(int a[],long long n){
int i,j;
int temp;
for(i=0;i<n;i++){
temp=a[i];
for(j=i;j>0&&a[j-1]>temp;j--){
a[j]=a[j-1];
}
a[j]=temp;
}
return ;
}
int main (){
int ary[MAXN];
int s[MAXN];
GetArray(ary);
start=clock();
for (int i=0;i<MAXN;i++){
QuickSort(ary,0,MAXN-1);
}
stop=clock();
duration=((double)(stop-start))/CLK_TCK;
printf ("The consumption of MAXN-QuickSort time: %lf\n",duration);
start=clock();
for (int i=0;i<MAXN/10;i++){
MergeSort(ary,s,0,MAXN-1);
}
stop=clock();
duration=((double)(stop-start))/CLK_TCK;
printf ("The consumption of MAXN/10-MergeSort time: %lf\n",duration);
start=clock();
for (int i=0;i<MAXN/10;i++){
HeapSort(ary,MAXN);
}
stop=clock();
duration=((double)(stop-start))/CLK_TCK;
printf ("The consumption of MAXN/10-HeapSort time: %lf\n",duration);
start=clock();
BubbleSort(ary,MAXN);
stop=clock();
duration=((double)(stop-start))/CLK_TCK;
printf ("The consumption of BubbleSort time: %lf\n",duration);
start=clock();
SelectSort(ary,MAXN);
stop=clock();
duration=((double)(stop-start))/CLK_TCK;
printf ("The consumption of SelectSort time: %lf\n",duration);
start=clock();
InsertSort(ary,MAXN);
stop=clock();
duration=((double)(stop-start))/CLK_TCK;
printf ("The consumption of MAXN-InsertSort time: %lf\n",duration);
return 0;
}