c++实现冒泡排序

时间:2022-12-29 22:02:53
# include<iostream>
#include
<stdio.h>

using namespace std;

void maopao(int *list){
int i,j,temp;
for(i=0;i<9;i++){
for(j=0;j<9-i;j++){
if(list[j]>list[j+1]){
temp
= list[j];
list[j]
= list[j+1];
list[j
+1] = temp;
//用于检测每一步的输出
/* cout<<"i等于"<<i<<"j等于"<<j<<endl;
for(int temp=0;temp<10;temp++){
cout<<list[temp]<<" ";
}
cout<<endl;
*/
}
}
}
}

int main(){
int list[10];
int n =9,m=0,i;
cout
<<"input 10 number"<<endl;
for(i=0;i<10;i++){
int tempvar;
cin
>>tempvar;
list[i]
=tempvar;
}
maopao(list);
for(i=0;i<10;i++){
cout
<<list[i]<<endl;
}
system(
"pause");
}

 

     每一次内部for循环都把当前的数中最大的放在最后一个(相当于一个反的传统的冒泡排序 );冒泡排序总是比较的是相邻之间的两个数并在需要时交换,因此冒泡排序是稳定的。时间复杂度O(n^2)