起泡排序

来源:互联网 发布:山西网络快报 编辑:程序博客网 时间:2024/04/20 08:39
#include
#include
typedef int KeyType;
typedef struct 
{
KeyType key;

} RcdType;

const int MAXSIZE=20;

typedef struct 
{
RcdType r[21];
int length;
}Sqlist,*SqList;


void initBubble(SqList p)
{

int i;
(*p).length=0;
for(i=0;i
{
(*p).r[i].key=0;
}
}

void Input(SqList q)
{
int i;
printf("please input length:\n");
scanf("%d\n",(*q).length);
for(i=1;i<=(*q).length;i++)
{
printf("please input number %d:",i);
scanf("%d\n",(*q).r[i].key);
}
printf("input finish\n");
}


void List(SqList q)
{
int i;
printf("the sort is:\n");
for(i=1;i<=(*q).length;i++)
{
printf("%d ",(*q).r[i].key);
}
printf("\n");
printf("the length is:\n");
printf("%d",(*q).length);
printf("\n");
}
void BubbleSort(SqList p)
{
RcdType w;
int i,j,last;
i=(*p).length;
while(i>1)
{
last=1;
for(j=1;j
{
if((*p).r[j+1].key<(*p).r[j].key)
{
w=(*p).r[j];
(*p).r[j]=(*p).r[j+1];
(*p).r[j+1]=w;
last=j;
}
}
i=last;
}
}


void main()
{
Sqlist L;
initBubble(&L);
Input(&L);
List(&L);
BubbleSort(&L);
List(&L);

}
0 0