我贴下代码,大家帮忙看下
#ifndef OALIST_H //no macro name given in #ifndef directive
#define OALIST_H
#include<stdlib.h>
#define MAXSIZE 1000
#define SORTNUM 6
#define FALSE 0
#define TRUE 1
typedef void (*Func)(long c,long s);
static Func Sort[SORTNUM]={
BubbleSort,InsertSort,SelectSort,QuickSort,ShellSort,HeapSort};
typedef int DataType[MAXSIZE+2];
static int Mix[]={ 0,1,4,16,64,128,256,512,4096};
DataType data;
DataType data2;
long com_times,shift_times;
int size;//
void BeforeSort()
{
com_times=shift_times=0;
}
int shift(int i,int j)
{
if(i<0||j<0||i>=size||j>=size)
return FALSE;
data[j]=data[i];
shift_times++;
}
int swap(int i,int j)
{
if(i<0||j<0||i>=size||j>=size)
return FALSE;
int temp;
temp=data[i];
data[i]=data[j];
data[j]=temp;
shift_times+=3;
}
int IsSmall(int i,int j)
{
if(i<0||j<0||i>=size||j>=size)
return FALSE;
com_times++;
if(data[i]<data[j])
return TRUE;
}
void Initlist(int n);
{
int i;
for(i=1;i<=n;i++)
data[i]=i;
com_times=shift_times=0;
size=n;
}
void RandomizeList(int d,int isInverse)
{
if(!isInverse)
InverseOrder();
//打乱这些数
int i;
for(i=1;i<=Mix[d];i++)
swap(random(size)+1,random(size)+1);
//把这些数储存在data2中,以备下次排序的调用
for(i=0;i<size;i++)
data2[i]=data[i];
}
void RecallList()//还原数据,作下次排序
{
int i;
for(i=1;i<=size;i++)
data[i]=data2[i];
}
void InverseOrder()
{
int i;
for(i=1;i<=size;i++)
data[i]=size-i+1;
}
//insert sort
void InsertSort(long &c,long &s)
{
int i,j;
BeforeSort();//reset the com_times and the shift_times
for(i=2;i<=size;i++)
{
data[0]=data[i];
j=i-1;
while(IsSmall(0,j))//find the insert position
{
shift(j,j+1);
j--;
}
shift(0,j);
}
c=com_times;
s=shift_times;
}
//shell sort
void ShellSort(long &c,long &s)
{
BeforeSort();
int i,h,j;
for(h=size/2;h>0;h=(h-1)/2)// 对每个增量序列进行排序 h=(h+1)/2
{
for(i=h+1;i<=size;i++)//对各个子序列进行排序
{
shift(i,0);
j=i-h;
while(j>0&&IsSmall(0,j))//find the insert position
{
shift(j,j+h);
j=j-h;
}
shift(0,j);
}//for i
}//for h
c=com_times;
s=shift_times;
}
// bubble sort
void BubbleSort(long &c,long &s)
{
int i,j;
BeforeSort();
for(i=1;i<size;i++)
for(j=size;j>i;j--)
if(IsSmall(j,j-1))
swap(j,j-1);
c=com_times;
s=shift_times;
}
//select sort
int SelectMinKey(int n)
{
int i,j;
j=n;
for(i=n+1;i<=size;i++)
if(IsSmall(i,j))
j=i;
return j;
}
void SelectSort(long &c,long &s)
{
BeforeSort();
int i,j;
for(i=1;i<size;i++)
{
j=SelectMinKey(i);//find the smallest key in the sequeue between i and size
if(j!=i)
swap(i,j);
}
c=com_times;
s=shift_times;
}//end select sort
//quick sort
void QSort(int high,int low)
{
data[0]=data[low];
int i,j;
i=low;
j=high;
while(i < j)
{
while(i < j && IsSmall(0,j))
j--;
shift(j,i);
while(i<j && IsSmall(i,0))
i++;
shift(i,j);
}
shift(0,i);
QSort(low,i-1);
QSort(i+1,high);
}
void QuickSort(long &c,long &s)
{
BeforeSort();
QSort(1,size);
c=com_times;
s=shift_times;
}// end
quick sort
// heap sort
void HeapAdjust(int s,int m)
{
int temp,j;
temp=data[s];
for(j=2*s;j<=m;j=j*2)//find the position of temp
{
if(j<m && IsSmall(j,j+1))
j++;
if(IsSmall(j,s))//do not need to adjust any maore
break;
shift(j,s);
s=j;//store the possible of temp
}//for j
data[s]=temp;
}
void HeapSort(long &c,long &s)
{
BeforeSort();
int i;
for(i=size/2;i>0;i--)// build the heap
HeapAdjust(i,size);
for(i=size;i>1;i--)//adjust the heap after sorting
{
swap(i,1);
HeapAdjust(1,i-1);
}//for i
c=com_times;
s=shift_times;
}
#endif