#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE_LENGTH_INSERT_SORT 7
#define MAXSIZE 10
#define ok 1
#define error 0
typedef struct sq{
int date[MAXSIZE+1];
int lenth;
}sqlist;
void swap(sqlist *L,int i,int j)
{
int temp=L->date[i];
L->date[i]=L->date[j];
L->date[j]=temp;
}
void insert(sqlist *l,int n)
{
int i;
int temp;
l->lenth = 0;
if(n>MAXSIZE)
{
printf("error no room");
exit(0);
}
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
l->date[i] = temp;
l->lenth++;
}
}
void BubbleSort0(sqlist *L)
{
int i,j;
for(i=1;i<L->lenth;i++)
{
for(j=i+1;j<=L->lenth;j++)
{
if(L->date[i]>L->date[j])
{
swap(L,i,j);
}
}
}
}
void BubbleSort1(sqlist *L)
{
int i,j;
for(i=1;i<L->lenth;i++)
{
for(j=L->lenth-1;j>=i;j--)
{
if(L->date[j]>L->date[j+1])
swap(L,j,j+1);
}
}
}
void BubbleSort2(sqlist *L)
{
int i,j;
int flag=1;
for(i=1;i<L->lenth&&flag;i++)
{
flag=0;
for(j=L->lenth-1;j>=i;j--)
{
swap(L,i,j);
flag=1;
}
}
}
void SelectSort(sqlist *L)
{
int i,j,min;
for(i=1;i<L->lenth;i++)
{
min = i;
for(j=i+1;j<=L->lenth;j++)
{
if(L->date[min]>L->date[j])
min=j;
}
if(i!=min)
swap(L,i,min);
}
}
void InsertSort(sqlist *L)
{
int i,j;
for(i=2;i<=L->lenth;i++)
{
if(L->date[i]<L->date[i-1])
{
L->date[0]=L->date[i];
for(j=i-1;L->date[j]>L->date[0];j--)
L->date[j+1]=L->date[j];
L->date[j+1]=L->date[0];
}
}
}
void ShellSort(sqlist *L)
{
int i,j;
int increment=L->lenth;
do
{
increment = increment/3+1;
for(i=increment+1;i<=L->lenth;i++)
{
if(L->date[i]<L->date[i-increment])
{
L->date[0]=L->date[i];
for(j=i-increment;j>0&&L->date[0]<L->date[j];j-=increment)
L->date[j+increment]=L->date[j];
L->date[j+increment]=L->date[0];
}
}
}
while(increment>1);
}
void HeapAdjust(sqlist *L,int s,int m)
{
int temp,j;
temp=L->date[s];
for(j=2*s;j<=m;j*=2)
{
if(j<m&&L->date[j]<L->date[j+1])
++j;
if(temp>=L->date[j])
break;
L->date[s]=L->date[j];
s=j;
}
L->date[s]=temp;
}
void HeapSort(sqlist *L)
{
int i;
for(i=L->lenth/2;i>0;i--)
HeapAdjust(L,i,L->lenth);
for(i=L->lenth;i>1;i--)
{
swap(L,1,i);
HeapAdjust(L,1,i-1);
}
}
void Merge(int SR[],int TR[],int i,int m,int n)
{
int j,k,l;
for(j=m+1,k=i;i<=m&&j<=n;k++)
{
if(SR[i]<SR[j])
TR[k]=SR[i++];
else
TR[k]=SR[j++];
}
if(i<=m)
{
for(l=0;l<=m-1;l++)
TR[k+l]=SR[i+l];
}
if(j<=n)
{
for(l=0;l<=n-j;l++)
TR[k+l]=SR[j+l];
}
}
void MSort(int SR[],int TR1[],int s,int t)
{
int m;
int TR2[MAXSIZE];
if(s==t)
TR1[s]=SR[s];
else
{
m=(s+t)/2;
MSort(SR,TR2,s,m);
MSort(SR,TR2,m+1,t);
Merge(TR2,TR1,s,m,t);
}
}
void MergeSort(sqlist *L)
{
MSort(L->date,L->date,1,L->lenth);
}
void MergePass(int SR[],int TR[],int s,int n)
{
int i=1;
int j;
while(i <= n-2*s+1)
{
Merge(SR,TR,i,i+s-1,i+2*s-1);
i=i+2*s;
}
if(i<n-s+1)
Merge(SR,TR,i,i+s-1,n);
else
for(j=i;j<=n;j++)
TR[j]=SR[j];
}
void MergeSort2(sqlist *L)
{
int *TR=(int *)malloc(L->lenth*sizeof(int));
int k=1;
while(k<L->lenth)
{
MergePass(L->date,TR,k,L->lenth);
k=2*k;
MergePass(TR,L->date,k,L->lenth);
k=2*k;
}
}
int Partition(sqlist *L,int low,int high)
{
int pivotkey;
int m= low+(high-low)/2;
if(L->date[low]>L->date[high])
swap(L,low,high);
if(L->date[m]>L->date[high])
swap(L,high,m);
if(L->date[m]>L->date[low])
swap(L,m,low);
pivotkey=L->date[low];
while(low<high)
{
while(low<high&&L->date[high]>=pivotkey)
high--;
swap(L,low,high);
while(low<high&&L->date[low]<=pivotkey)
low++;
swap(L,low,high);
}
return low;
}
int Partition1(sqlist *L,int low,int high)
{
int pivotkey;
int m= low+(high-low)/2;
if(L->date[low]>L->date[high])
swap(L,low,high);
if(L->date[m]>L->date[high])
swap(L,high,m);
if(L->date[m]>L->date[low])
swap(L,m,low);
pivotkey=L->date[low];
L->date[0]=pivotkey;
while(low<high)
{
while(low<high&&L->date[high]>=pivotkey)
high--;
L->date[low]=L->date[high];
while(low<high&&L->date[low]<=pivotkey)
low++;
L->date[high]=L->date[low];
}
L->date[low]=L->date[0];
return low;
}
void QSort(sqlist *L,int low,int high)
{
int pivot;
if(low<high)
{
pivot=Partition(L,low,high);
QSort(L,low,pivot-1);
QSort(L,pivot+1,high);
}
}
void QSort1(sqlist *L,int low,int high)
{
int pivot;
if((high-low)>MAXSIZE_LENGTH_INSERT_SORT)
{
pivot=Partition(L,low,high);
QSort1(L,low,pivot-1);
QSort1(L,pivot+1,high);
}
else
InsertSort(L);
}
void QSort2(sqlist *L,int low ,int high)
{
int pivot;
if((high-low)>MAXSIZE_LENGTH_INSERT_SORT)
{
while(low<high)
{
pivot=Partition1(L,low,high);
QSort2(L,low,pivot-1);
low=pivot+1;
}
}
else
InsertSort(L);
}
void QuickSort(sqlist *L)
{
QSort2(L,1,L->lenth);
}
void print(sqlist l)
{
int i;
for(i=1;i<=l.lenth;i++)
printf("%d ",l.date[i]);
if(i==l.lenth)
printf("\n");
}
int main()
{
sqlist *l;
int n;
int m;
l=(sqlist*)malloc(sizeof(sqlist));
l->lenth=0;
printf("请输入链表元素个数(不大于10个)");
scanf("%d",&n);
printf("请输入链表元素\n");
insert(l,n);
QuickSort(l);
print(*l);
return 0;
}