冒泡排序

来源:互联网 发布:电影院简单数据库设计 编辑:程序博客网 时间:2024/05/11 10:31
//冒泡排序,其实就是将n个数中最大的数放在a[n],
/*再将n-1个数中最大的数的放在a[n-1],以此类推*/ 

#include<iostream>
using namespace std;
#define N 100
typedef int KeyType;
typedef char OtherType;
typedef struct RecordType{
KeyType key;
OtherType other_data;
}type;
void BubbleSort(RecordType r[],int length){
int n=length;
for(int i=1;i<n;++i){
for(int j=1;j<=n-i;j++){
if(r[j].key>r[j+1].key){
type x=r[j];
r[j]=r[j+1];
r[j+1]=x;
}
}
}
}
int main(){
type a[N];;
int n;

     cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i].key;
BubbleSort(a,n);
for(int i=1;i<=n;i++)
cout<<a[i].key<<" ";
return 0;
0 0
原创粉丝点击