【算法】多种排序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0

typedef struct{
int *base;
int length;
}SqList;




//插入排序
int InsertSort(SqList &L) {
int *p,*q;
for(p=L.base+2;p<=L.base+L.length;p++)
if(*p<*(p-1)) 
{*L.base=*p;*p=*(p-1);
for(q=p-2;q>=L.base;q--)
if(*q>*L.base)*(q+1)=*q;
else {*(q+1)=*L.base;break;}}
printf("插入排序的结果为:\n");
for(p=L.base+1;p<=L.base+L.length;p++)
printf("%d ",*p);
return OK;
}




//选择排序
int SelectSort(SqList &L1) 
{
int i,j,a,temp,*p;
for(i=1;i<=L1.length;i++)
{a=i;
{for(j=i+1;j<=L1.length;j++)
if(L1.base[j]<L1.base[i]&&L1.base[j]<L1.base[a]) a=j;}
temp=L1.base[i];
L1.base[i]=L1.base[a];
L1.base[a]=temp;
}
printf("选择排序的结果为:\n");
for(p=L1.base+1;p<=L1.base+L1.length;p++)
printf("%d ",*p);
return OK;
}



//气泡排序
int BubbleSort(SqList &L2)
{
int *p,*q;
int n=0,temp;
for(p=L2.base+1;p<L2.base+L2.length;p++)
{for(q=p+1;q<=L2.base+L2.length;q++)
if(*p>*q){temp=*p;*p=*q;*q=temp;n++;}
if(n==0)break;
n=0;}
    printf("气泡排序的结果为:\n");
for(p=L2.base+1;p<=L2.base+L2.length;p++)
printf("%d ",*p);
return OK;
}





//快速排序的分割函数
int Partition(SqList &L3,int low,int high)
{
int i,j;
i=low;
j=high;
L3.base[0]=L3.base[i];
while(i<j)
{
while(i<j&&L3.base[j]>=L3.base[0])
{
j--; 
}
L3.base[i]=L3.base[j];
while(i<j&&L3.base[i]<=L3.base[0])
{
i++;
}
L3.base[j]=L3.base[i];
}
L3.base[i]=L3.base[0];
return i;
}
//快速排序
int QuickSort(SqList &L,int low,int high)
{
int i,j,k;
i=low;
j=high;
if(i<j){
k=Partition(L,i,j);   
        QuickSort(L,i,k-1);
        QuickSort(L,k+1,j);
}
return OK;
}



//调整成最大堆
int HeapAdjust(SqList &L4,int last)
{
int i,k=0,temp;
if(last%2==1)
{for(i=last/2;i>0;i--)
{if(L4.base[i]<L4.base[2*i]){temp=L4.base[i];L4.base[i]=L4.base[2*i];L4.base[2*i]=temp;}
if(L4.base[i]<L4.base[2*i+1]){temp=L4.base[i];L4.base[i]=L4.base[2*i+1];L4.base[2*i+1]=temp;}}}
else{for(i=last/2;i>0;i--)
if(2*i==last){if(L4.base[i]<L4.base[2*i]){temp=L4.base[i];L4.base[i]=L4.base[2*i];L4.base[2*i]=temp;}}
else{if(L4.base[i]<L4.base[2*i]){temp=L4.base[i];L4.base[i]=L4.base[2*i];L4.base[2*i]=temp;}
if(L4.base[i]<L4.base[2*i+1]){temp=L4.base[i];L4.base[i]=L4.base[2*i+1];L4.base[2*i+1]=temp;}}}
return OK;
}
//堆排序
int HeapSort(SqList &L4,int last)
{
int temp,last1=last;
int *p;
while(last1!=1)
{
HeapAdjust(L4,last1);
temp=L4.base[1];
L4.base[1]=L4.base[last1];
L4.base[last1]=temp;
last1--;
}
printf("堆排序的结果为:\n");
for(p=L4.base+1;p<=L4.base+L4.length;p++)
printf("%d ",*p);
return OK;
}


//数组合并及拷贝函数
void Merge_Copy(SqList &L5,SqList &L6,int l,int m,int r)
{
int i,j=m+1,k=l;
for(i=1;i<=L5.length;i++)
L6.base[i]=L5.base[i];
i=l;
while((i<=m)&&(j<=r))
{if(L5.base[i]<=L5.base[j]) L6.base[k++]=L5.base[i++];
else L6.base[k++]=L5.base[j++];}
if(i>m)for(int q=j;q<=r;q++)L6.base[k++]=L5.base[q];
else for(int q=i;q<=m;q++)L6.base[k++]=L5.base[q];
for(i=1;i<=L5.length;i++)
L5.base[i]=L6.base[i];
}
//递归合并排序函数
void MergeSort(SqList &L5,SqList &L6,int left,int right)
{if(left<right){
int i=(left+right)/2;
MergeSort(L5,L6,left,i);
MergeSort(L5,L6,i+1,right);
Merge_Copy(L5,L6,left,i,right);
}
}

//主函数
void main()
{
    SqList L,L1,L2,L3,L4,L5,L6;
int i,n;
int *p;
printf("请输入要排序的数据个数:\n");
scanf("%d",&n);
L.base=(int*)malloc((n+1)*sizeof(int));
L1.base=(int*)malloc((n+1)*sizeof(int));
L2.base=(int*)malloc((n+1)*sizeof(int));
L3.base=(int*)malloc((n+1)*sizeof(int));
    L4.base=(int*)malloc((n+1)*sizeof(int));
L5.base=(int*)malloc((n+1)*sizeof(int));
L6.base=(int*)malloc((n+1)*sizeof(int));
L1.length=L2.length=L3.length=L4.length=L5.length=L.length=n;
printf("请输入要排序的%d个数据:\n",n);
for(p=L.base+1;p<=L.base+L.length;p++)
scanf("%d",p);
for(i=1;i<=n;i++)
{L1.base[i]=L2.base[i]=L3.base[i]=L4.base[i]=L5.base[i]=L.base[i];}
printf("您要排序的数据为:\n");
for(p=L.base+1;p<=L.base+L.length;p++)
printf("%d ",*p);
printf("\n",*p);
int select,flag=1;
printf("==============================菜单============================\n");
printf("=                                                    1.插入排序                                                  =\n");
        printf("=                                                    2.选择排序                                                  =\n");
printf("=                                                    3.气泡排序                                                  =\n");
printf("=                                                    4.快速排序                                                  =\n");
printf("=                                                    5.堆排序                                                      =\n");
printf("=                                                    6.递归合并排序                                           =\n");
printf("=                                                    7.退出操作                                                  =\n");
printf("===========================支持乱序选择========================\n");
while(flag)
{
printf("\n                  请选择菜单中的排序种类:\n");
scanf("%d",&select);
switch(select)
{
case 1:
InsertSort(L);
break;
case 2:
SelectSort(L1);
break;
case 3:
BubbleSort(L2);
break;
case 4:
QuickSort(L3,1,n);
printf("快速排序的结果为:\n");
for(p=L3.base+1;p<=L3.base+L3.length;p++)
printf("%d ",*p);
break;
case 5:
HeapSort(L4,n);
break;
case 6:
MergeSort(L5,L6,1,n);
printf("递归合并排序的结果为:\n");
for(p=L5.base+1;p<=L5.base+L5.length;p++)
printf("%d ",*p);
break;
case 7:
flag=0;break;
    default:
printf("您输入的数据有误!!!请重新输入!!!\n");
}

}
}


你可能感兴趣的:(算法,排序)