产生10个随机数,用冒泡法排序,从大到小输出(降序)

冒泡法排序:

 1 #include <iostream>
 2 #include<ctime>
 3 #include<cstdlib>
 4 using namespace std;
 5 
 6   int main() {
 7     int a[10] ,i,j,t;
 8     srand(time(0));                    //srand()函数根据当前时间产生随机数
 9     t=rand();
10     for(i=0;i<=10;i++){
11         a[i] = rand()%100 +100;        //10个随机整数,区间为【100,199】
12         cout<<a[i]<<"\t";
13     }
14     cout<<endl;
15 
16     //冒泡法排序
17     for(i=0; i<=9; i++){
18         for(j=0; j<=10-i; j++){
19             if(a[j] < a[j+1]){
20               t=a[j]; a[j]=a[j+1]; a[j+1]=t;         //交换a[j]与a[j+1]
21             }
22         }
23     }
24 
25     //排序后,输出
26     for(i=0; i<=10; i++){
27         cout<<a[i]<<"\t";
28     }
29     
30     return 0;
31 }

猜你喜欢

转载自www.cnblogs.com/52yu/p/12918374.html