當前位置:編程學習大全網 - 編程軟體 - C++編程題 排序 求大神指教~~~

C++編程題 排序 求大神指教~~~

隨機給10個數,並排序輸出(冒泡法)。希望對妳有用

#include<iostream.h>

#include<stdlib.h>

void input(int a[],int N)//隨機輸出10個數

{

for(int i=0;i<10;i++)

a[i]=rand()%100;

}

void bubblesort(int a[],int N)//排序

{

int temp;

for(int i=1;i<10;i++)

{

for(int j=0;j<10-i;j++)

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

void print (int a[],int N)//輸出

{

for(int i=0;i<N;i++)

cout<<a[i]<<" ";

cout<<endl;

}

const int n=10;

void main()

{

int num[n];

input(num,n);

cout<<"before sorted data:"<<endl;

print(num,n);

bubblesort(num,n);

cout<<"sorted data"<<endl;

print(num,n);

}

  • 上一篇:計算機碩士需要學哪些專業計算機碩士有哪些專業
  • 下一篇:2015年通遼職業學院教師招聘報名時間多會兒?
  • copyright 2024編程學習大全網