C++实现冒泡排序

时间:2022-02-25 09:49:01

C++实现冒泡排序:

//冒泡排序
#include<iostream>
#include<time.h>
#include<algorithm>
using namespace std;
void bubblesort(int *a,int num){
int i,j;
for(i=1;i!=num;++i)
{
for(j=0;j!=num-i;++j)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
}
int main()
{
cout<<"要输入的数组的个数"<<endl;
int num;
cin>>num;
cin.clear();
srand(time(0));
int *elem=new int[num];
for(int i=0;i!=num;++i)
elem[i]=rand()%100;
bubblesort(elem,num);
for(int i=0;i!=num;++i)
cout<<elem[i]<<" ";
cout<<endl;
delete [] elem;
return 0;
}