冒泡算法
1
#include
<
iostream
>
2 #include < string >
3 #include < algorithm >
4 using namespace std;
5
6 void bubbleSort( int * ary,size_t size){
7 bool flag = true ;
8 for (size_t i = 0 ;i < size;i ++ ){
9 for (size_t j = 0 ;j < size - i - 1 ;j ++ ){
10 if (ary[j] > ary[j + 1 ]){
11 swap(ary[j],ary[j + 1 ]);
12 flag = false ;
13 }
14 }
15 if (flag == true )
16 break ;
17 flag = true ;
18 }
19 }
20
21
22 int main(){
23 int test[ 8 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
24 bubbleSort(test, 8 );
25 copy(test,test + 8 ,ostream_iterator < int > (cout, " " ));
26 system( " pause " );
27 }
2 #include < string >
3 #include < algorithm >
4 using namespace std;
5
6 void bubbleSort( int * ary,size_t size){
7 bool flag = true ;
8 for (size_t i = 0 ;i < size;i ++ ){
9 for (size_t j = 0 ;j < size - i - 1 ;j ++ ){
10 if (ary[j] > ary[j + 1 ]){
11 swap(ary[j],ary[j + 1 ]);
12 flag = false ;
13 }
14 }
15 if (flag == true )
16 break ;
17 flag = true ;
18 }
19 }
20
21
22 int main(){
23 int test[ 8 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
24 bubbleSort(test, 8 );
25 copy(test,test + 8 ,ostream_iterator < int > (cout, " " ));
26 system( " pause " );
27 }