#include <stdio.h>
//希尔排序:不稳定,是直接插入排序的改进
void ShellSort(int a[], int n)
{
int h,i,j,t;
for(h = n/2; h > 0; h /= 2)//h为步长,下面算法由直接插入算法以h代替1得来
for(i = h; i < n; i++)
{
t=a[i];
j=i-h;
while(j>=0 && t<a[j])
{
a[j+h]=a[j];
j-=h;
}
a[j+h]=t;
}
}
//快速排序:不稳定,是冒泡排序的改进
void QuickSort(int a[],int low,int high)
{
int i,j,t;
if(low<high)
{
i=low;j=high;t=a[low];//对low到high以a[low]为基准划分
while(i<j)
{
while(i<j && a[j]>t)
j--;
if(i<j)
a[i++]=a[j];
while(i<j && a[i]<=t)
i++;
if(i<j)
a[j--]=a[i];
}
a[i]=t;
QuickSort(a,low,i-1);
QuickSort(a,i+1,high);
}
}
//堆排序:不稳定,是直接选择排序的改进
void sift(int a[],int N,int s)
{
int i,j,t;
i=s;j=2*i+1;
t=a[s];
while(j<N)
{
if(j<N-1 && a[j]<a[j+1])
j++;
if(t<a[j])
{
a[i]=a[j];
i=j;
j=2*i+1;
}
else
break;
}
a[i]=t;
}
void HeapSort(int a[],int N)
{
int i,j,t;
//建初始堆
for(i=N/2;i>=0;i--)
sift(a,N,i);
for(j=N-1;j>=1;j--)
{
t=a[0];
a[0]=a[j];
a[j]=t;
sift(a,j,0);
}
}
void main()
{
int a[]={93,35,29,45,48,82,76,17};
for(int i=0;i<8;i++)
printf("%d ",a[i]);
printf("/n");
ShellSort(a,8);
for(i=0;i<8;i++)
printf("%d ",a[i]);
printf("/n");
QuickSort(a,0,7);
for(i=0;i<8;i++)
printf("%d ",a[i]);
printf("/n");
HeapSort(a,8);
for(i=0;i<8;i++)
printf("%d ",a[i]);
}