void BubbleSort0(SqList *L)
{
int i,j;
for(i=1;i<L->length;i++)
{
for(j=i+1;j<=L->length;j++)
{
if(L->r[i]>L->r[j])
{
swap(L,i,j);
}
}
}
}
void BubbleSort(SqList *L)
{
int i,j;
for(i=1;i<L->length;i++)
{
for(j=L->length-1;j>=i;j--)
{
if(L->r[j]>L->r[j+1])
{
swap(L,j,j+1);
}
}
}
}
void BubbleSort2(SqList *L)
{
int i,j;
Status flag=TRUE;
for(i=1;i<L->length && flag;i++)
{
flag=FALSE;
for(j=L->length-1;j>=i;j--)
{
if(L->r[j]>L->r[j+1])
{
swap(L,j,j+1);
flag=TRUE;
}
}
}
}
void SelectSort(SqList *L)
{
int i,j,min;
for(i=1;i<L->length;i++)
{
min = i;
for (j = i+1;j<=L->length;j++)
{
if (L->r[min]>L->r[j])
min = j;
}
if(i!=min)
swap(L,i,min);
}
}
void InsertSort(SqList *L)
{
int i,j;
for(i=2;i<=L->length;i++)
{
if (L->r[i]<L->r[i-1])
{
L->r[0]=L->r[i];
for(j=i-1;L->r[j]>L->r[0];j--)
L->r[j+1]=L->r[j];
L->r[j+1]=L->r[0];
}
}
}
/* 对顺序表L作希尔排序 */
void ShellSort(SqList *L)
{
int i,j,k=0;
int increment=L->length;
do
{
increment=increment/3+1;/* 增量序列 */
for(i=increment+1;i<=L->length;i++)
{
if (L->r[i]<L->r[i-increment])/* 需将L->r[i]插入有序增量子表 */
{
L->r[0]=L->r[i]; /* 暂存在L->r[0] */
for(j=i-increment;j>0 && L->r[0]<L->r[j];j-=increment)
L->r[j+increment]=L->r[j]; /* 记录后移,查找插入位置 */
L->r[j+increment]=L->r[0]; /* 插入 */
}
}
printf(" 第%d趟排序结果: ",++k);
print(*L);
}
while(increment>1);
}
void HeapAdjust(SqList *L,int s,int m)
{
int temp,j;
temp=L->r[s];
for(j=2*s;j<=m;j*=2)
{
if(j<m && L->r[j]<L->r[j+1])
++j;
if(temp>=L->r[j])
break;
L->r[s]=L->r[j];
s=j;
}
L->r[s]=temp;
}
void HeapSort(SqList *L)
{
int i;
for(i=L->length/2;i>0;i--)
HeapAdjust(L,i,L->length);
for(i=L->length;i>1;i--)
{
swap(L,1,i);
HeapAdjust(L,1,i-1);
}
}
int Partition1(SqList *L,int low,int high)
{
int pivotkey;
int m = low + (high - low) / 2;
if (L->r[low]>L->r[high])
swap(L,low,high);
if (L->r[m]>L->r[high])
swap(L,high,m);
if (L->r[m]>L->r[low])
swap(L,m,low);
pivotkey=L->r[low];
L->r[0]=pivotkey;
while(low<high)
{
while(low<high&&L->r[high]>=pivotkey)
high--;
L->r[low]=L->r[high];
while(low<high&&L->r[low]<=pivotkey)
low++;
L->r[high]=L->r[low];
}
L->r[low]=L->r[0];
return low;
}
void QSort1(SqList *L,int low,int high)
{
int pivot;
if((high-low)>MAX_LENGTH_INSERT_SORT)
{
while(low<high)
{
pivot=Partition1(L,low,high);
QSort1(L,low,pivot-1);
low=pivot+1;
}
}
else
InsertSort(L);
}
void QuickSort1(SqList *L)
{
QSort1(L,1,L->length);
}