#include <iostream>
#include <iterator>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <list>
#include <sys/timeb.h>
#define TIMEB timeb
#define _FUNC cout<<__FUNCTION__<<endl
#define print(arr,n) copy(arr,arr+n,ostream_iterator<T>(cout," "));\
cout<<endl;
using
namespace std;
const
char *
const red =
"
\033[0;40;31m
";
const
char *
const green=
"
\033[0;40;32m
";
const
char *
const normal =
"
\033[0m
";
template <
class T>
void insert_sort(T a[],
int n)
{
for(
int i=
1;i<n;i++) {
T t=a[i];
int j;
for(j=i-
1;j>=
0&&a[j]>t;j--) {
a[j+
1]=a[j];
}
a[j+
1]=t;
}
}
template <
class T>
void binary_insert_sort(T a[],
int n){
for(
int i=
1;i<n;i++){
int low=
0;
int high=i-
1;
T t=a[i];
int mid;
while(low<=high){
mid=(low+high)/
2;
if(t<a[mid])
high=mid-
1;
else
low=mid+
1;
}
for(
int j=i;j>mid;j--)
a[j]=a[j-
1];
a[low]=t;
}
}
template <
class T>
void shell_sort(T a[],
int n)
{
int gap=n/
2;
bool flag=
true;
while(gap>
1||flag)
{
flag=
false;
for(
int i=
0;i+gap<n;i++)
if(a[i]>a[i+gap])
{
swap(a[i],a[i+gap]);
flag=
true;
}
if(gap>
1)
gap/=
2;
}
}
template <
class T>
void shell_sort2(T a[],
int n){
int gap=n/
2;
while(gap>
0){
for(
int i=gap;i<n;i++){
T t=a[i];
int j;
for(j=i-gap;j>=
0&&a[j]>t;j-=gap)
a[j+gap]=a[j];
a[j+gap]=t;
}
gap/=
2;
}
}
template <
class T>
void select_sort(T a[],
int n)
{
for(
int i=
0;i<n-
1;i++)
{
T min=a[i];
int index=i;
for(
int j=i+
1;j<n;j++)
if(a[j]<min)
{
min=a[j];
index=j;
}
swap(a[i],a[index]);
}
}
template <
class T>
void select_sort2(T a[],
int n)
{
for(
int i=n-
1;i>
0;i--){
for(
int j=
0;j<i;j++)
if(a[j]>a[i])
swap(a[j],a[i]);
}
}
template <
class T>
void select_sort3(T a[],
int n)
{
bool flag=
true;
for(
int i=n-
1;i>
0&&flag;i--){
flag=
false;
for(
int j=
0;j<i;j++)
if(a[j]>a[i])
swap(a[j],a[i]),flag=
true;
}
}
template <
class T>
void adjust(T b[],
int m,
int n){
int j=m;
int k=
2*m;
while(k<=n){
if(k<n&&b[k]<b[k+
1])
k++;
if(b[j]<b[k])
swap(b[j],b[k]);
j=k;
k*=
2;
}
}
template <
class T>
void heap_sort(T a[],
int n){
T *b=a-
1;
for(
int i=n/
2;i>=
1;i--)
adjust(b,i,n);
for(
int i=n-
1;i>=
1;i--){
swap(b[
1],b[i+
1]);
adjust(b,
1,i);
}
}
template <
class T>
void bubble_sort(T a[],
int n)
{
for(
int i=n-
1;i>
0;i--)
for(
int j=
0;j<i;j++)
if(a[j]>a[j+
1])
swap(a[j],a[j+
1]);
}
template <
class T>
void bubble_sort2(T a[],
int n)
{
bool flag=
true;
for(
int i=n-
1;i>
0&&flag;i--){
flag=
false;
for(
int j=
0;j<i;j++)
if(a[j]>a[j+
1])
swap(a[j],a[j+
1]),flag=
true;
}
}
template <
class T>
void qsort(T a[],
int l,
int r){
T pvt=a[(l+r)/
2];
int i=l,j=r;
while(i<=j){
while(a[i]<pvt)
i++;
while(a[j]>pvt)
j--;
if(i<=j){
if(i!=j)
swap(a[i],a[j]);
i++;
j--;
}
}
if(j>l)
qsort(a,l,j);
if(i<r)
qsort(a,i,r);
}
template <
class T>
void quick_sort(T a[],
int n){
qsort(a,
0,n-
1);
}
template <
class T>
void merge(T a[],T b[],
int l,
int m,
int r){
int i,j,k;
i=l;
j=m+
1;
k=l;
while(i<=m&&j<=r){
if(a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=m)
b[k++]=a[i++];
while(j<=r)
b[k++]=a[j++];
for(
int s=l;s<=r;s++)
a[s]=b[s];
}
template <
class T>
void msort(T a[],T b[],
int l,
int r){
if(l<r){
int m=(l+r)/
2;
msort(a,b,l,m);
msort(a,b,m+
1,r);
merge(a,b,l,m,r);
}
}
template <
class T>
void merge_sort(T a[],
int n){
T *b=
new T[n];
msort(a,b,
0,n-
1);
delete[] b;
}
template <
class T>
void merge_pass(T x[],T y[],
int s,
int n){
int i=
0;
while(i+
2*s-
1<n){
merge(x,y,i,i+s-
1,i+
2*s-
1);
i+=
2*s;
}
if(i+s<n)
merge(x,y,i,i+s-
1,n-
1);
else
for(
int j=i;j<=n-
1;j++)
y[j]=x[j];
}
template <
class T>
void merge_sort2(T a[],
int n){
T *b=
new T[n];
int s=
1;
while(s<n){
merge_pass(a,b,s,n);
s+=s;
merge_pass(b,a,s,n);
s+=s;
}
delete[] b;
}
template <
class T>
void merge_sort3(T a[],
int n){
vector<
int> st;
for(
int i=
0;i<n-
1;i++){
if(a[i]>a[i+
1])
st.push_back(i);
}
st.push_back(n-
1);
T *b=
new T[n];
int l,m,r;
l=
0;
if(!st.empty())
{
m=st.front();
st.erase(st.begin());
}
while(!st.empty()){
r=st.front();
st.erase(st.begin());
merge(a,b,l,m,r);
m=r;
}
delete [] b;
}
template <
class T>
void merge_sort4(T a[],
int n){
int *pos=
new
int[n];
int k=
0;
for(
int i=
0;i<n-
1;i++){
if(a[i]>a[i+
1])
pos[k++]=i;
}
pos[k++]=n-
1;
T *b=
new T[n];
int l,m,r;
l=
0;
int p=
0;
if(p<k)
m=pos[p++];
while(p<k){
r=pos[p++];
merge(a,b,l,m,r);
m=r;
}
delete [] b;
}
template <
class T>
void rank(T arr[],
int n,
int r[])
{
for(
int i=
0;i<n;i++)
r[i]=
0;
for(
int i=
1;i<n;i++){
for(
int j=
0;j<i;j++)
{
if(arr[j]<=arr[i])
r[i]++;
else
r[j]++;
}
}
}
template <
class T>
void rank_sort(T arr[],
int n)
{
int *r=
new
int[n];
rank(arr,n,r);
for(
int i=
0;i<n;i++)
{
while(r[i]!=i)
{
T t=r[i];
swap(arr[i],arr[t]);
swap(r[i],r[t]);
}
}
delete[] r;
}
template <
class T>
void rank_sort2(T a[],
int n){
int *r=
new
int[n];
rank(a,n,r);
T *u=
new T[n];
for(
int i=
0;i<n;i++)
u[r[i]]=a[i];
for(
int i=
0;i<n;i++)
a[i]=u[i];
delete[] r;
delete[] u;
}
int maxbits(
int a[],
int n){
int d=
0;
for(
int i=
0;i<n;i++){
int b=
1;
int r=a[i];
while(r/
10>
0){
b++;
r/=
10;
}
if(d<b)
d=b;
}
return d;
}
void radix_sort(
int a[],
int n){
int d=maxbits(a,n);
int *temp=
new
int[n];
int *count=
new
int[
10];
int adix=
1;
for(
int b=
1;b<=d;b++){
for(
int i=
0;i<
10;i++)
count[i]=
0;
for(
int i=
0;i<n;i++){
int k=(a[i]/adix)%
10;
count[k]++;
}
for(
int i=
1;i<
10;i++)
count[i]+=count[i-
1];
for(
int i=n-
1;i>=
0;i--){
int k=(a[i]/adix)%
10;
count[k]--;
temp[count[k]]=a[i];
}
for(
int i=
0;i<n;i++)
a[i]=temp[i];
adix*=
10;
}
delete[] temp;
delete[] count;
}
void radix_sort2(
int a[],
int n){
int bits=maxbits(a,n);
list<
int> x(a,a+n);
int range=
10;
vector<list<
int> > bin(range);
list<
int> y;
list<
int>::iterator ite;
int adix=
1;
for(
int i=
0;i<bits;i++){
for(ite=x.begin();ite!=x.end();ite++){
int d=(*ite/adix)%
10;
bin[d].push_back(*ite);
}
vector<list<
int> >::iterator ite2;
y.clear();
for(ite2=bin.begin();ite2!=bin.end();++ite2){
for(ite=ite2->begin();ite!=ite2->end();++ite)
y.push_back(*ite);
ite2->clear();
}
x=y;
adix*=
10;
}
int i=
0;
for(ite=x.begin();ite!=x.end();ite++)
a[i++]=*ite;
}
template <
class T>
void sort_test(
void (*_sort)(T*,
int)){
const
int N=
10;
T orig[N];
T standard[N];
T arr[N];
srand(time(
0));
for(
int j=
0;j<
18;j++){
for(
int i=
0;i<N;i++)
orig[i]=rand()%
100;
cout<<
"
bef:
";
print(orig,N);
copy(orig,orig+N,standard);
sort(standard,standard+N);
cout<<
"
std:
";
print(standard,N);
copy(orig,orig+N,arr);
_sort(arr,N);
cout<<
"
aft:
";
print(arr,N);
if(equal(standard,standard+N,arr))
printf(
"
%sOK%s\n
",green,normal);
else
printf(
"
%sNO%s\n
",red,normal);
}
}
template <
class T>
void clock_test()
{
void(*_sort[])(T*,
int)={insert_sort,binary_insert_sort,shell_sort,shell_sort2,select_sort,
select_sort2,select_sort3,heap_sort,bubble_sort,bubble_sort2,quick_sort,
merge_sort,merge_sort2,merge_sort3,merge_sort4,rank_sort,rank_sort2};
int len=
sizeof(_sort)/
sizeof(_sort[
0]);
clock_t start,finish;
//
cout<<CLOCKS_PER_SEC<<endl;
cout<<
"
鐩存帴鎻掑叆\t鎶樺崐鎻掑叆\t甯屽皵鎺掑簭\t
"<<endl;
int step=
100;
for(
int k=
100;k<=
100000;k+=step)
{
const
int N=k;
T arr[N];
double tm[N];
//
srand(time(0));
for(
int i=
0;i<N;i++)
arr[i]=rand()%
1000;
for(
int i=
0;i<len;i++) {
start=clock();
_sort[i](arr,N);
finish=clock();
tm[i]=(finish-start)*
1.0/CLOCKS_PER_SEC;
}
cout<<k<<
'
\t
';
for(
int i=
0;i<len;i++)
cout<<tm[i]<<
'
\t
';
cout<<endl;
}
}
template <
class T>
void time_test()
{
void(*_sort[])(T*,
int)={insert_sort,binary_insert_sort,shell_sort,shell_sort2,select_sort,
select_sort2,select_sort3,heap_sort,bubble_sort,bubble_sort2,quick_sort,
merge_sort,merge_sort2,merge_sort3,merge_sort4,rank_sort,rank_sort2};
int len=
sizeof(_sort)/
sizeof(_sort[
0]);
struct TIMEB ts1,ts2;
time_t t_sec,t_ms,ti;
cout<<
"
鐩存帴鎻掑叆\t鎶樺崐鎻掑叆\t甯屽皵鎺掑簭\t
"<<endl;
int step=
100;
for(
int k=
100;k<=
100000;k+=step)
{
const
int N=k;
T arr[N];
double tm[N];
for(
int i=
0;i<N;i++)
arr[i]=rand()%
1000;
cout<<k<<
'
\t
';
for(
int i=
0;i<len;i++) {
ftime(&ts1);
_sort[i](arr,N);
ftime(&ts2);
t_sec=ts2.time-ts1.time;
t_ms=ts2.millitm-ts1.millitm;
ti=t_sec*
1000+t_ms;
cout<<ti<<
'
\t
';
}
cout<<endl;
}
}
int main()
{
//
sort_test(radix_sort2);
//
clock_test<int>();
time_test<
int>();
}