C语言排序
Posted 电气自动化资料库
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言排序相关的知识,希望对你有一定的参考价值。
#include
#define maxsize 30
typedef int keytype;
typedef struct
{
keytype key;
int data;
}datatype;
typedef struct
{
datatype r[maxsize];
int length;
int listsize;//表的初始分配存储容量
}sqlist,*psqlist;
int seqsearch(sqlist s,keytype k)//顺序查找
{
int i;
for(i=0;i<s.length;i++)< span=""></s.length;i++)<>
if(s.r[i].key==k)
return(i);
return(-1);
}
int seqsearch_(sqlist s,keytype k)//带前哨站顺序查找
{
int n,i=0;
n=s.length;
s.r[n].key=k;
while(s.r[i].key!=k)
i++;
if(i==n)return -1;
else return i;
}
int binsearch(sqlist s,keytype k)//折半查找
{
int low,mid,high;
low=0;
high=s.length-1;
while(low<=high)< span="">
{
mid=(low+high)/2;
if(s.r[mid].key==k) return(mid);
else if(s.r[mid].key>k) high=mid-1;
else low=mid+1;
}
return(-1);
}
void straightinsertsort(sqlist *s)//直接插入
{
int i,j;
datatype c;
for(i=1;i
{
c=s->r[i];
j=i-1;
while(c.key
{s->r[j+1]=s->r[j];
j--;}
s->r[j+1]=c;
}
}
void binaryinsertsort(sqlist *s)//折半插入
{
int low,high,mid;
datatype c;
for(int i=1;i
{c=s->r[i];
low=1;
high=i-1;
while(low<=high)< span="">
{
mid=(low+high)/2;
if(c.key>=s->r[mid].key)
low=mid+1;
else high=mid-1;
}
for(int j=i-1;j>=high+1;j--)
s->r[j+1]=s->r[j];
s->r[high+1]=c;}
}
void shellinsert(sqlist *s,int gap)//希尔排序
{
int i,j;
datatype c;
for(i=gap;i
if(s->r[i].key
{
c=s->r[i];
for(j=i-gap;j>=0&&c.key
s->r[j+gap]=s->r[j];
s->r[j+gap]=c;
}
}
void shellsort(sqlist *s,int gaps[],int t)
{
int k;
for(k=0;k<t;t++)< span=""></t;t++)<>
shellinsert(s,gaps[k]);
}
void bubblesort(sqlist *s)//冒泡排序
{
int i,j;
datatype c;
for(i=0;i
for(j=1;j
if(s->r[j].key
{
c=s->r[j];
s->r[j]=s->r[j-1];
s->r[j-1]=c;
}
}
int quicksort1(sqlist *s,int low,int high)//快速查找非递归
{
keytype p;
datatype c;
c=s->r[low];
p=s->r[low].key;
while(low<high)< span=""></high)<>
{
while(low
high--;
s->r[low]=s->r[high];
while(low
low++;
s->r[high]=s->r[low];
}
s->r[low]=c;
return low;
}
void quicksort(sqlist *s,int low,int high)//递归快速查找
{
int p;
if(low<high)< span=""></high)<>
{
p=quicksort1(s,low,high);
quicksort(s,low,p-1);
quicksort(s,p+1,high);
}
}
psqlist init_sqlist()//初始化
{
psqlist pl;
pl=(psqlist)malloc(sizeof(sqlist));
if(pl)pl->length=0;
return(pl);
}
int insert_sqlist(psqlist pl,int i,datatype x)//插入数据
{
int j;
if(!pl)
{printf("error");
return(-2);}
if(pl->length>maxsize)
{printf("wrong");
return(-1);}
for(j=pl->length-1;j>i-1;j--)
pl->r[j+1]=pl->r[j];
pl->r[i-1]=x;
pl->length++;
return(1);
}
main()
{
// int i;
// int s,m;
// datatype a[30]={1,2,3,4,5,6,7,8,9,10,20,19,18,17,16,15,14,13,12,11,21,22,23,24,25,26,27,28,29,30};
psqlist list=init_sqlist();
printf("输入30个数字:");
int n=30;
list->length=n;
int i;
for(i=0;i
scanf("%d",&list->r[i]);
printf("\n");
int s;
bubblesort(list);
printf("冒泡排序:");
for(i=0;i
printf("%d ",list->r[i]);
printf("\n");
s=seqsearch(*list,6);//顺序查找
printf("直接查找数字6:%d\n",s+1);
s=binsearch(*list,5);
printf("折半查找数字5:%d\n",s+1);
//插入排序
straightinsertsort(list);
printf("直接插入排序:");
for(i=0;i
printf("%d ",list->r[i]);
printf("\n");
binaryinsertsort(list);
printf("折半插入排序:");
for(i=0;i
printf("%d ",list->r[i]);
printf("\n");
quicksort1(list,0,4);
printf("快速排序:");
for(i=0;i
printf("%d ",list->r[i]);
printf("\n");
int g[3]={3,2,1};
shellsort(list,g,3);
printf("希尔排序:");
for(i=0;i
printf("%d ",list->r[i]);
printf("\n");
return 0;
}
以上是关于C语言排序的主要内容,如果未能解决你的问题,请参考以下文章