几种排序法的头文件.doc
文本预览下载声明
typedef struct
{
KeyType key;
}DataType;
void InsertSort(DataType a[],int n)//直接插入排序
{
int i,j;
DataType temp;
for(i=0;in-1;i++)
{
temp=a[i+1];
j=i;
while(j-1temp.keya[j].key)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
void ShellSort(DataType a[],int n,int d[],int numOfD)//希尔排序方法
{
int i,j,k,m,span;
DataType temp;
for(m=0;mnumOfD;m++)
{
span=d[m];
for(k=0;kspan;k++)
{
for(i=k;in-span;i=i+span)
{
temp=a[i+span];
j=i;
while(j-1temp.key=a[j].key)
{
a[j+span]=a[j];
j=j-span;
}
a[j+span]=temp;
}
}
}
}
void SelectSort(DataType a[],int n)//直接选择排序
{
int i,j,small;
DataType temp;
for(i=0;in-1;i++)
{
small=i;
for(j=i+1;jn;j++)
if(a[j].keya[small].key)small=j;
if(small!=i)
{
temp=a[i];
a[i]=a[small];
a[small]=temp;
}
}
}
//交换排序
void BubbleSort(DataType a[],int n)//冒泡排序
{
int i,j,flag=1;
DataType temp;
for(i=1;inflag==1;i++)
{
flag=0;
for(j=0;jn-1;j++)
{
if(a[j].keya[j+1].key)
{
flag=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void QuickSort(DataType a[],int low,int high)//快速排序
{
int i=low,j=high;
DataType temp=a[low];
while(ij)
{
while(ijtemp.key=a[j].key)j--;
if(ij)
{
a[i]=a[j];
i++;
}
while(ija[i].keytemp.key)i++;
if(ij)
{
a[j]=a[i];
j--;
}
}
a[i]=temp;
if(lowi)QuickSort(a,low,i-1);
if(ihigh)QuickSort(a,j+1,high);
}
显示全部