起泡排序算法练习
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 420 (219 users) Total Accepted: 241 (216 users) Special Judge: No
Description
利用起泡排序算法实现递增排序。
Input
输入一组整数
Output
输出递增的整数序列
Sample Input
2 1 5 4 3
Sample Output
1 2 3 4 5
Author
金恩海
#include<stdio.h>
#include<bits/stdc++.h>
#include<iostream>
#include<stdlib.h>
#define maxn 1005
#define ElemType int
#define OK 1
using namespace std;
typedef struct
{
ElemType key;
} RcdType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
} sqlist;
sqlist start()
{
sqlist l;
l.elem=(int *)malloc(maxn *sizeof(ElemType));
l.length=0;
l.listsize=maxn;
return l;
}
void insert1 (sqlist &l,ElemType e)
{
l.elem[l.length]=e;
l.length++;
}
int BublleSort(sqlist &L)//冒泡
{
for(int i=0; i<L.length; i++)
{
for(int j=0; j<L.length-1; j++)
{
if(L.elem[j]>L.elem[j+1])
{
int temp=L.elem[j+1];
L.elem[j+1]=L.elem[j];
L.elem[j]=temp;
}
}
}
}
void output(sqlist &l)//输出
{
for(int i=0; i<l.length; i++)
printf("%d ",l.elem[i]);
}
int main()
{
sqlist l;
l=start();
int a;
while(~scanf("%d",&a))
{
insert1(l,a);
}
BublleSort(l);
output(l);
}