2010年冬天上课,老师留的作业要求课后上机实现排序的算法,现在一年半已经过去了。
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
#define random(x)(rand()%(x))
#define N 10000
class timer//单位ms
{
public:
void start()
{
start_t=clock();
}
clock_t time()
{
return (clock()-start_t);
}
private:
clock_t start_t;
};
timer TIMER;
/*--------------------------直接插入排序----------------------*/
void InsertSort(int a[])
{
TIMER.start();
for(int i=1; i<N; i++)
{
int temp=a[i];
int j;
for(j=i; j>0&&temp<a[j-1]; j--)
a[j]=a[j-1];
a[j]=temp;
}
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*--------------------------直接插入排序-----------------------*/
/*--------------------------希尔排序---------------------------*/
void ShellSort(int a[])
{
TIMER.start();
for(int gap=N/2; gap; gap=gap/2)
for(int i=gap; i<N; i++)
{
int temp=a[i];
int j;
for(j=i; j>=gap&&temp<a[j-gap]; j-=gap)
a[j]=a[j-gap];
a[j]=temp;
}
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*--------------------------希尔排序--------------------------*/
/*--------------------------冒泡排序--------------------------*/
void BubbleSort(int a[])
{
TIMER.start();
//bool exchange=true;
for(int i=1; i<N; i++)
for(int j=N-1; j>=i; j--)
{
//exchange=false;
if(a[j-1]>a[j])
{
swap(a[j-1],a[j]);
//exchange=true;
}
}
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*--------------------------冒泡排序--------------------------*/
/*--------------------------快速排序--------------------------*/
int Partition(int a[], int low, int high)
{
//a[0]=a[low];
int pivotkey=a[low];
while(low<high)
{
while(low<high&&a[high]>=pivotkey)
--high;
a[low]=a[high];
while(low<high&&a[low]<=pivotkey)
++low;
a[high]=a[low];
}
//a[low]=a[0];
a[low]=pivotkey;
return low;
}
void QSort(int a[], int low, int high)
{
if(low<high)
{
int pivotloc=Partition(a, low, high);
QSort(a, low, pivotloc-1);
QSort(a, pivotloc+1, high);
}
}
void QuickSort(int a[])
{
TIMER.start();
QSort(a, 0, N-1);
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*--------------------------快速排序--------------------------*/
/*------------------------直接选择排序------------------------*/
void SelectSort(int a[])
{
TIMER.start();
for(int i=0; i<N; i++)
{
int j, k;
for(j=i+1, k=i; j<N; j++)
if(a[j]<a[k])
k=j;
if(k!=i)
swap(a[k],a[i]);
}
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*------------------------直接选择排序------------------------*/
/*---------------------------堆排序---------------------------*/
void HeapAdjust(int a[], int i, int n)
{
int child=2*i+1;
int temp=a[i];
while(child<n)
{
if(child<n-1&&a[child]<a[child+1])
child++;
if(temp>=a[child])
break;
a[i]=a[child];
i=child;
child=2*i+1;
}
a[i]=temp;
}
void HeapSort(int a[])
{
TIMER.start();
int i;
for(i=(N-2)/2; i>=0; i--)
HeapAdjust(a, i, N);
for(i=N-1; i>0; i--)
{
swap(a[0],a[i]);
HeapAdjust(a, 0, i);
}
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*---------------------------堆排序---------------------------*/
/*--------------------------归并排序--------------------------*/
void Merge(int a[], int p, int q, int r)
{
int* temp=(int*)malloc((r-p+1)*sizeof(int));
int begin1, begin2, end1, end2, k;
begin1=p;
end1=q;
begin2=q+1;
end2=r;
k=0;
while((begin1<=end1)&&(begin2<=end2))
{
if(a[begin1]<a[begin2])
{
temp[k]=a[begin1];
begin1++;
}
else
{
temp[k]=a[begin2];
begin2++;
}
k++;
}
while(begin1<=end1)
{
temp[k++]=a[begin1++];
//k++;
//begin1++;
}
while(begin2<=end2)
{
temp[k++]=a[begin2++];
//k++;
//begin2++;
}
for(k=0; k<(r-p+1); k++)
a[p+k]=temp[k];
free(temp);
}
void MSort(int a[], int first, int last)
{
//TIMER.start();
int mid=0;
if(first<last)
{
mid=(first+last)/2;
MSort(a, first, mid);
MSort(a, mid+1, last);
Merge(a, first, mid, last);
}
//cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
void MergeSort(int a[])
{
TIMER.start();
MSort(a, 0, N-1);
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
/*--------------------------归并排序--------------------------*/
/*--------------------------基数排序--------------------------*/
/*
void RadixSort(int a[])
{
TIMER.start();
int i, j, k, n, lsd;
int temp[N][N]={0};
int order[N]={0};
n=1;
k=0;
while(n<=10000)//对于随机数怎样确定n的范围取值
{
for(i=0; i<N; i++)
{
lsd=((a[i]/n)%10);
temp[lsd][order[lsd]]=a[i];
order[lsd]++;
}
for(i=0; i<N; i++)
{
if(order[i]!=0)
for(j=0; j<order[i]; j++)
{
a[k]=temp[i][j];
k++;
}
order[i]=0;
}
n*=10;
k=0;
}
cout<<"\tTimeSpared:"<<TIMER.time()<<"ms"<<endl;
}
*/
/*--------------------------基数排序--------------------------*/
int main()
{
int i;
int* ascending=new int[N];//升序序列
int* descending=new int[N];//降序序列
int* randomness=new int[N];//随机序列
for(i=0; i<N; i++)
{
ascending[i]=i;
randomness[i]=i;
descending[i]=N-i-1;
}
for(i=0; i<N; i++)
swap(randomness[i],randomness[random(N)]);
cout<<"-------------直接插入排序-------------"<<endl;
cout<<"Sort ascending N="<<N;InsertSort(ascending);
cout<<"Sort randomness N="<<N;InsertSort(randomness);
cout<<"Sort descending N="<<N;InsertSort(descending);
cout<<"-------------希尔排序-------------"<<endl;
cout<<"Sort ascending N="<<N;ShellSort(ascending);
cout<<"Sort randomness N="<<N;ShellSort(randomness);
cout<<"Sort descending N="<<N;ShellSort(descending);
cout<<"-------------起泡排序-------------"<<endl;
cout<<"Sort ascending N="<<N;BubbleSort(ascending);
cout<<"Sort randomness N="<<N;BubbleSort(randomness);
cout<<"Sort descending N="<<N;BubbleSort(descending);
cout<<"-------------快速排序-------------"<<endl;
cout<<"Sort ascending N="<<N;QuickSort(ascending);
cout<<"Sort randomness N="<<N;QuickSort(randomness);
cout<<"Sort descending N="<<N;QuickSort(descending);
cout<<"-------------直接选择排序-------------"<<endl;
cout<<"Sort ascending N="<<N;SelectSort(ascending);
cout<<"Sort randomness N="<<N;SelectSort(randomness);
cout<<"Sort descending N="<<N;SelectSort(descending);
cout<<"-------------堆排序-------------"<<endl;
cout<<"Sort ascending N="<<N;HeapSort(ascending);
cout<<"Sort randomness N="<<N;HeapSort(randomness);
cout<<"Sort descending N="<<N;HeapSort(descending);
cout<<"-------------归并排序-------------"<<endl;
cout<<"Sort ascending N="<<N;MergeSort(ascending);
cout<<"Sort randomness N="<<N;MergeSort(randomness);
cout<<"Sort descending N="<<N;MergeSort(descending);
/*
cout<<"-------------基数排序-------------"<<endl;
cout<<"Sort ascending N="<<N;RadixSort(ascending);
cout<<"Sort randomness N="<<N;RadixSort(randomness);
cout<<"Sort descending N="<<N;RadixSort(descending);
*/
}