Skip to content

Commit eb5d484

Browse files
daliang111willrlzhang
and
willrlzhang
authored
feat: add bubbleSort in c++(#551)
Co-authored-by: willrlzhang <willrlzhang@tencent.com>
1 parent 12eeb53 commit eb5d484

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

basic/sorting/BubbleSort/README.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,78 @@ func main() {
9393
}
9494
```
9595

96-
<!-- tabs:end -->
96+
### **C++**
97+
```c++
98+
#include <iostream>
99+
#include <vector>
100+
#include <string>
101+
102+
using namespace std;
103+
104+
/* 简单版本 */
105+
void bubblesort( vector<int> & vec )
106+
{
107+
for ( int i = 0; i < vec.size() - 1; i++ )
108+
{
109+
for ( int j = 0; j < vec.size() - i - 1; j++ )
110+
{
111+
if ( vec[j] > vec[j + 1] )
112+
{
113+
swap( vec[j], vec[j + 1] );
114+
}
115+
}
116+
}
117+
}
118+
119+
120+
/* 改进版本 */
121+
void bubblesort1( vector<int> & vec )
122+
{
123+
for ( int i = 0; i < vec.size() - 1; i++ )
124+
{
125+
bool exchange = false;
126+
for ( int j = 0; j < vec.size() - i - 1; j++ )
127+
{
128+
if ( vec[j] > vec[j + 1] )
129+
{
130+
swap( vec[j], vec[j + 1] );
131+
exchange = true;
132+
}
133+
}
134+
135+
if ( !exchange )
136+
{
137+
break;
138+
}
139+
}
140+
}
141+
142+
143+
void printvec( const vector<int> & vec, const string & strbegin = "", const string & strend = "" )
144+
{
145+
cout << strbegin << endl;
146+
for ( auto val : vec )
147+
{
148+
cout << val << "\t";
149+
}
150+
151+
cout << endl;
152+
cout << strend << endl;
153+
}
154+
155+
156+
int main( void )
157+
{
158+
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
159+
printvec( vec );
160+
161+
bubblesort1( vec );
162+
163+
printvec( vec, "after sort", "" );
164+
}
165+
166+
167+
```
97168
98169
## 算法分析
99170
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
/* 简单版本 */
8+
void bubblesort( vector<int> & vec )
9+
{
10+
for ( int i = 0; i < vec.size() - 1; i++ )
11+
{
12+
for ( int j = 0; j < vec.size() - i - 1; j++ )
13+
{
14+
if ( vec[j] > vec[j + 1] )
15+
{
16+
swap( vec[j], vec[j + 1] );
17+
}
18+
}
19+
}
20+
}
21+
22+
23+
/* 改进版本 */
24+
void bubblesort1( vector<int> & vec )
25+
{
26+
for ( int i = 0; i < vec.size() - 1; i++ )
27+
{
28+
bool exchange = false;
29+
for ( int j = 0; j < vec.size() - i - 1; j++ )
30+
{
31+
if ( vec[j] > vec[j + 1] )
32+
{
33+
swap( vec[j], vec[j + 1] );
34+
exchange = true;
35+
}
36+
}
37+
38+
if ( !exchange )
39+
{
40+
break;
41+
}
42+
}
43+
}
44+
45+
46+
void printvec( const vector<int> & vec, const string & strbegin = "", const string & strend = "" )
47+
{
48+
cout << strbegin << endl;
49+
for ( auto val : vec )
50+
{
51+
cout << val << "\t";
52+
}
53+
54+
cout << endl;
55+
cout << strend << endl;
56+
}
57+
58+
59+
int main( void )
60+
{
61+
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
62+
printvec( vec );
63+
64+
bubblesort1( vec );
65+
66+
printvec( vec, "after sort", "" );
67+
}
68+
69+

0 commit comments

Comments
 (0)