#include<iostream.h>
#include<stdlib.h>
typedef int keytype ;
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<=(b))
#define LQ(a,b) ((a)>(b))
#define l_size 11
typedef struct
{
keytype key;
int next;
}selemtype;
typedef struct
{
selemtype elem[l_size+1];
int len;
}sstable;
void creat_(sstable &ST)
{
ST.len=l_size+1;
}
void cinn(sstable &ST);
void insert_sort(sstable &ST);//直接插入排序
void Insert_sort(sstable &ST);//折半插入排序
void display(sstable &ST);
void arrange(sstable &ST)/////////////////////////???
{
int p;
p=ST.elem[0].next;
for(int i=1;i<=ST.len;++i)
{
while(p<i)p=ST.elem[p].next;
int q;
q=ST.elem[p].next ;
if(p!=i)
{
selemtype int1;
int1=ST.elem[p] ;
ST.elem[p]=ST.elem[i];
ST.elem[i]=int1;
ST.elem[i].next=p;
}
p=q;
}
}
//////////////////////
void shellinsert(sstable &ST,int dk);//希尔排序
void shellsort(sstable &ST,int a[],int t);
///////////////////
////////////////////////////////////////////////////
int partition(sstable &ST,int low,int high)//快速排序
{
int positionkey;
positionkey=ST.elem[low].key;
while(low<high)
{
while(low<high&&ST.elem[high].key>=positionkey)--high;
selemtype st;
st=ST.elem[low];
ST.elem[low]=ST.elem[high];
ST.elem[high]=st;
while(low<high&&ST.elem[low].key<=positionkey)++low;
selemtype st1;
st1=ST.elem[low];
ST.elem[low]=ST.elem[high];
ST.elem[high]=st1;
}
return low;
}
//上函数改进后
int partition1(sstable &ST,int low,int high)//快速排序
{
int positionkey;
ST.elem[0]=ST.elem[low];
positionkey=ST.elem[low].key;
while(low<high)
{
while(low<high&&ST.elem[high].key>=positionkey)--high;
ST.elem[low]=ST.elem[high];
while(low<high&&ST.elem[low].key<=positionkey)++low;
ST.elem[high]=ST.elem[low];
}
return low;
}
void Qsort(sstable &ST,int low,int high)
{
int positionkey;
if(low<high)
{
positionkey=partition1(ST,low,high);
Qsort(ST,low,positionkey-1);
Qsort(ST,positionkey+1,high);
}
}
void Quicksort(sstable &ST)
{
Qsort(ST,1,ST.len);
}
/////////////////////////////////////////////////////////////////////堆排序
void heapadjust(sstable &ST,int s,int m)
{
selemtype rc;
rc=ST.elem[s];
for(int j=2*s;j<=m;j*=2)
{
if(j<m&<(ST.elem[j].key,ST.elem[j+1].key))++j;
if(!LT(rc.key,ST.elem[j].key))break;
ST.elem[s]=ST.elem[j];s=j;
}
ST.elem[s]=rc;
}
void heapsort(sstable &ST)
{
int i;
for( i=ST.len/2;i>0;i--)
heapadjust(ST,i,ST.len );
for(i=ST.len;i>1;--i)
{
selemtype st;
st =ST.elem[1];
ST.elem[1]=ST.elem[i];
ST.elem[i]=st;
heapadjust(ST,1,i-1);
}
}
//////////////////////////////////////
void main()
{
sstable ST,ST1,ST2,ST3,ST4,ST5;
cinn(ST);
cout<<"ST= ";
display(ST);
insert_sort(ST);
cout<<"After Aort ST= ";
display(ST);
cinn(ST1);
cout<<"ST1= ";
display(ST1);
cout<<"After Aort ST= ";
Insert_sort(ST1);
display(ST1);
cinn(ST2);
cout<<"ST2= ";
display(ST2);
cout<<"After Arrange ST2= 希尔排序 /n";
for(int i=9;i>=2;i--)
{
shellinsert(ST2,i);
display(ST2);/**/
}
int a[]={5,3,1};
cout<<"ST3= ";
cinn(ST3);
display(ST3);
shellsort(ST3,a,3);
cout<<"After Aort ST3= ";
display(ST3);
cout<<"ST4= ";
cinn(ST4);
display(ST4);
Quicksort(ST4);
cout<<"After Aort ST4= ";
display(ST4);
cout<<"ST5= ";
cinn(ST5);
display(ST5);
heapsort(ST5);
cout<<"After Aort ST5= ";
display(ST5);/**/
}
void shellinsert(sstable &ST,int dk)
{
for(int i=dk+1;i<=ST.len;++i)
if(LT(ST.elem[i].key,ST.elem[i-dk].key))
{
ST.elem[0]=ST.elem[i];
for(int j=i-dk;j>0&<(ST.elem[0].key,ST.elem[j].key) ;j-=dk)
ST.elem[j+dk]=ST.elem[j];
ST.elem[j+dk]=ST.elem[0];
}
}
void shellsort(sstable &ST,int a[],int t)
{
for(int k=0;k<t;++k)
shellinsert(ST,a[k]);
}
void Insert_sort(sstable &ST)
{
int low, high,m;
for(int i=2;i<=ST.len;++i)
{
ST.elem[0]=ST.elem[i];
low=1;high=i-1;
while(low<=high)
{
m=(low+high)/2;
if(LT(ST.elem[0].key,ST.elem[m].key ))high=m-1;
else low=m+1;
}
for(int j=i-1;j>=high+1;--j) ST.elem[j+1]=ST.elem[j];
ST.elem[high+1]=ST.elem[0];
}
}
void cinn(sstable &ST)
{
//ST.elem[0].key=0;
ST.len=l_size+1;
for(int i=1;i<=l_size;i++)
ST.elem[i].key=l_size-i;
}
void insert_sort(sstable &ST)///////////直接插入排序
{
for(int i=2;i<=ST.len;++i)
if(LT(ST.elem[i].key,ST.elem[i-1].key ))
{
ST.elem[0]=ST.elem[i];
ST.elem[i]=ST.elem[i-1];
for(int j=i-2;LT(ST.elem[0].key ,ST.elem[j].key);--j)
ST.elem[j+1]=ST.elem[j];
ST.elem[j+1]=ST.elem[0];
}
}
void display(sstable &ST)
{
for(int j=1;j<=l_size;j++)
cout<<ST.elem[j].key<<" ";
cout<<endl;
}