# include <iostream>冒泡排序java代码详解
# include <cstdlib>
# include <ctime>
using namespace std;
void sort(int [], int);
int main()
{
int i, a[10];
srand(int (time(0)));
for(i=0; i<10; i++)
a[i] = rand() % 100;//这个事电脑取随机数参考C++P146页
for(i=0; i<10; i++)
cout << a[i] << " ";
cout << endl;
sort(a, 10);
for(i=0; i<10; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
void sort(int a[], int size)
{
int k, t, work;
for(int pass = 1; pass<size; pass++)
{
work = 1;
for(int i=0; i<size-pass; i++)
{
if(a[i] > a[i+1])
{
t = a[i];
a[i] = a[i+1];
a[i+1] = t;
work = 0;
}
}
if(work) break;
}
return ;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论