/***********************************************************/
/* 学生:刘中华
/* 日期:2014-1-2
/* 内容:涉及了string类的初步使用,冒泡法排序,学生名字与成绩对应
/*
/*
/***********************************************************/
#include <iostream>
#include<string>
using namespace std;
//两个函数 bubble_sort 和 output_array 的声明
void bubble_sort(int m[],string p[],int n);
void output_array(int m[],string p[],int n);
int main( )
{
int a[5]={86,76,62,58,77};
int b[15]={27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};
string name[20]={"aa","bb","cc","dd","ee"};
bubble_sort(a,name,5); //用冒泡法按降序排序 a 中元素
cout<<"排完1个"<<endl;
output_array(a,name,5); //输出排序后的数组
//bubble_sort(b,15); //用冒泡法按降序排序 b 中元素
//cout<<"排完2个"<<endl;
//output_array(b,15); //输出排序后的数组
return 0;
}
void bubble_sort(int m[],string p[],int n)
{
int t,i,j,k;
string tt;
for(i=0;i<n-1;i++)
{
for(j=n-1,k=0;j>0;j--,k++)
if(m[k]<m[k+1])//降序排列,交换
{
t=m[k];
m[k]=m[k+1];
m[k+1]=t;
//tt.swap(p[k]);
//p[k].swap(p[k+1]);
//p[k+1].swap(tt);
tt=p[k];
p[k]=p[k+1];
p[k+1]=tt;
}
}
}
void output_array(int m[],string p[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<m[i]<<" "<<p[i]<<endl;
}
冒泡法排序
最新推荐文章于 2016-08-07 21:53:41 发布