#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "runCLock.h"
#define DEBUG_INSERT 1
#define DEBUG_BUBBLE 1
#define DEBUG_QUICK 1
#define DEBUG_SELECT 1
#define DEBUG_HEAP 1
#define DEBUG_MERGE 1
#define DEBUG_SHELL 1
using namespace std;
// data defination
const int LEN=60000;
const int BOUNDARY=14997;
//basic OP
void init(int s[], const int LEN)
{
srand((unsigned)time(NULL));
for(int i=0; i<LEN; i++)
{
s[i]= rand()%BOUNDARY;
}
}
void print(int s[], const int LEN)
{
for(int i=0; i<LEN; i++)
{
cout<<s[i]<<" ";
}
cout<<endl;
}
//insertSort
void insertSort(int s[], const int LEN)
{
int i,j;
int tmp;
for(i=1; i<LEN; i++)
{
tmp=s[i];
j=i;
while(j>=1 && tmp<s[j-1])
{
s[j]=s[j-1];
j--;
}
s[j]=tmp;
}
}
//shellSort
void shellSort(int s[],const int LEN)
{
int i,j,k;
int temp;
int h;
h=1;
while(h<LEN)
{
h=3*h+1;
}
while(h>=1)
{
h=h/3;
for(k=0; k<h; k++)
{
for(i=k+h;i<LEN;i+=h)
{
temp=s[i];
j=i;
while(j>=h && temp<s[j-h])
{
s[j]=s[j-h];
j-=h;
}
s[j]=temp;
}
}
}
}
//bubbleSort
void bubbleSort(int s[], const int LEN)
{
int tmp;
for(int i=0; i<=LEN-1; i++)
{
for(int j=0; j<=LEN-2-i; j++)
{
if(s[j]>s[j+1])
{
tmp=s[j];
s[j]=s[j+1];
s[j+1]=tmp;
}
}
}
}
//quickSort
int partition(int s[], int high, int low)
{
int pivot;
pivot=s[low];
while(low<high)
{
while(low<high&&pivot<=s[high])
{
high--;
}
s[low]=s[high];
while(low<high&&pivot>=s[low])
{
low++;
}
s[high]=s[low];
}
s[low]=pivot;
return low;
}
void qSort(int s[], int high, int low)
{
int pivotLoc;
if(high>low) //except for only 1 element
{
pivotLoc=partition(s,high,low);
qSort(s,high,pivotLoc+1);
qSort(s,pivotLoc-1,low);
}
}
void quickSort(int s[], int LEN)
{
qSort(s,LEN-1,0);
}
//selectSort()
void selectSort(int s[], const int LEN)
{
int tmp;
for(int i=0; i<LEN-1; i++)
{
int j=i;
for(int k=i+1; k<LEN; k++)
{
if(s[k]<s[j])
{
j=k;
}
}
if(j!=i)
{
tmp=s[j];
s[j]=s[i];
s[i]=tmp;
}
}
}
//heapSort() bigHeap
void heapAdjust(int s[], int node, int len)
{
int largeIndex=-1;
int lc,rc;
int tmp;
lc=node*2;
rc=node*2+1;
if(lc<=len && s[lc]>s[node])
{
largeIndex=lc;
}
else
{
largeIndex=node;
}
if(rc<=len && s[rc]>s[largeIndex])
{
largeIndex=rc;
}
if(largeIndex!=node)
{
tmp=s[largeIndex];
s[largeIndex]=s[node];
s[node]=tmp;
heapAdjust(s,largeIndex,len);
}
}
void heapSort(int s[], int len) //1...len
{
int index=(len)/2;
int tmp;
//build heap
for(int i=index; i>=1; i--)
{
heapAdjust(s,i,len);
}
//sort
for(int i=len; i>1; i--)
{
tmp=s[i];
s[i]=s[1];
s[1]=tmp;
heapAdjust(s,1,i-1);
}
}
//merge Sort
void merge(int s[], int low, int mid, int high) //low...mid mid+1...high
{
int left[mid-low+1], right[high-mid];
int ss,ls,le,rs,re;
ss=low;
ls=0;
le=mid-low;
rs=0;
re=high-mid-1;
for(int i=low; i<=mid; i++)
{
left[i-low]=s[i];
}
for(int i=mid+1; i<=high; i++)
{
right[i-mid-1]=s[i];
}
while(ls<=le && rs<=re)
{
if(left[ls]<right[rs])
{
s[ss]=left[ls];
ls++;
}
else
{
s[ss]=right[rs];
rs++;
}
ss++;
}
while(ls<=le)
{
s[ss]=left[ls];
ss++;
ls++;
}
while(rs<=re)
{
s[ss]=right[rs];
ss++;
rs++;
}
}
void mSort(int s[], int low, int high)
{
if(high>low)
{
int mid=(high+low)/2;
mSort(s,low,mid);
mSort(s,mid+1,high);
merge(s,low,mid,high);
}
}
void mergeSort(int s[], int LEN)
{
mSort(s,0,LEN-1);
}
void shellSortChai(int *a,const int length){
int i,j,k,h;
int temp;
h=1;
while(h<length)
h=3*h+1;
while(h>0){
h=h/3;
for(i=0;i<h;i++){
for(j=h;j<length;j+=h)
for(k=j;j>0&&a[j]<a[j-h];j-=h){
temp = a[j];
a[j] = a[j-h];
a[j-h] = temp;
}
}
}
}
int main()
{
int s[LEN];
runClock aClock;
#if DEBUG_INSERT
cout<<"init:";
init(s,LEN);
//print(s,LEN);
cout<<"insertSort:";
aClock.clear();
aClock.start();
insertSort(s,LEN);
//print(s,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
#endif
#if DEBUG_SHELL
cout<<"init:";
init(s,LEN);
//print(s,LEN);
cout<<"shellSort:";
aClock.clear();
aClock.start();
shellSort(s,LEN);
//print(s,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
#endif
#if DEBUG_BUBBLE
cout<<"init:";
init(s,LEN);
//print(s,LEN);
cout<<"bubbleSort:";
aClock.clear();
aClock.start();
bubbleSort(s,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
//print(s,LEN);
#endif
#if DEBUG_QUICK
cout<<"init:";
init(s,LEN);
//print(s,LEN);
cout<<"quickSort:";
aClock.clear();
aClock.start();
quickSort(s,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
//print(s,LEN);
#endif
#if DEBUG_SELECT
cout<<"init:";
init(s,LEN);
//print(s,LEN);
cout<<"selectSort:";
aClock.clear();
aClock.start();
selectSort(s,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
//print(s,LEN);
#endif
#if DEBUG_HEAP
cout<<"init:";
init(s,LEN);
int headSrc[LEN+1];
memcpy(headSrc+1,s,LEN*sizeof(int));
//print(headSrc+1,LEN);
cout<<"heapSort:";
aClock.clear();
aClock.start();
heapSort(headSrc,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
//print(headSrc+1,LEN);
#endif
#if DEBUG_MERGE
cout<<"init:";
init(s,LEN);
//print(s,LEN);
cout<<"mergeSort:";
aClock.clear();
aClock.start();
mergeSort(s,LEN);
aClock.end();
printf("\ntime:%lf\n",aClock.result());
//print(s,LEN);
#endif
return 0;
}
#ifndef RUNCLOCK_H_INCLUDED
#define RUNCLOCK_H_INCLUDED
#include <ctime>
class runClock
{
public:
runClock();
void start();
void end();
double result();
void clear();
private:
clock_t startTime, endTime;
};
#endif // RUNCLOCK_H_INCLUDED
#include "runClock.h"
runClock::runClock()
{
startTime=0.0;
endTime=0.0;
}
void runClock::start()
{
startTime=clock();
}
void runClock::end()
{
endTime=clock();
}
double runClock::result()
{
return double(endTime-startTime)/CLOCKS_PER_SEC;
}
void runClock::clear()
{
startTime=0.0;
endTime=0.0;
}
运行结果: