Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 05246b4

Browse files
daliang111willrlzhangyanglbme
authored
feat: quick sort in cpp (doocs#559)
Co-authored-by: willrlzhang <willrlzhang@tencent.com> Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent f094ed3 commit 05246b4

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

‎basic/sorting/QuickSort/Main.cpp‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
void printvec( const vector<int> &vec, const string &strbegin = "", const string &strend = "" )
6+
{
7+
cout << strbegin << endl;
8+
for ( auto val : vec )
9+
{
10+
cout << val << "\t";
11+
}
12+
13+
cout << endl;
14+
cout << strend << endl;
15+
}
16+
17+
18+
int partition( vector<int> & vec, int left, int right )
19+
{
20+
if ( left >= right )
21+
{
22+
return left;
23+
}
24+
25+
int base = vec[left];
26+
while ( left < right )
27+
{
28+
while ( left < right && vec[right] >= base )
29+
{
30+
right--;
31+
}
32+
if ( left >= right )
33+
{
34+
break;
35+
}
36+
37+
vec[left] = vec[right];
38+
39+
while ( left < right && vec[left] <= base )
40+
{
41+
left++;
42+
}
43+
44+
if ( left >= right )
45+
{
46+
break;
47+
}
48+
49+
vec[right] = vec[left];
50+
}
51+
52+
vec[left] = base;
53+
return left;
54+
}
55+
56+
57+
void quicksort( vector<int> & vec, int left, int right )
58+
{
59+
if ( left >= right )
60+
{
61+
return;
62+
}
63+
64+
int idx = partition( vec, left, right );
65+
quicksort( vec, left, idx - 1 );
66+
quicksort( vec, idx + 1, right );
67+
}
68+
69+
70+
int main( void )
71+
{
72+
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
73+
printvec( vec );
74+
quicksort( vec, 0, vec.size() - 1 );
75+
printvec( vec, "after insert sort" );
76+
return 0;
77+
}
78+
79+
80+

‎basic/sorting/QuickSort/README.md‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,85 @@ fn main() -> io::Result<()> {
288288
}
289289
```
290290

291+
### **C++**
292+
293+
```cpp
294+
#include <iostream>
295+
#include <vector>
296+
297+
using namespace std;
298+
void printvec( const vector<int> &vec, const string &strbegin = "", const string &strend = "" )
299+
{
300+
cout << strbegin << endl;
301+
for ( auto val : vec )
302+
{
303+
cout << val << "\t";
304+
}
305+
306+
cout << endl;
307+
cout << strend << endl;
308+
}
309+
310+
311+
int partition( vector<int> & vec, int left, int right )
312+
{
313+
if ( left >= right )
314+
{
315+
return left;
316+
}
317+
318+
int base = vec[left];
319+
while ( left < right )
320+
{
321+
while ( left < right && vec[right] >= base )
322+
{
323+
right--;
324+
}
325+
if ( left >= right )
326+
{
327+
break;
328+
}
329+
330+
vec[left] = vec[right];
331+
332+
while ( left < right && vec[left] <= base )
333+
{
334+
left++;
335+
}
336+
337+
if ( left >= right )
338+
{
339+
break;
340+
}
341+
342+
vec[right] = vec[left];
343+
}
344+
345+
vec[left] = base;
346+
return left;
347+
}
348+
349+
350+
void quicksort( vector<int> & vec, int left, int right )
351+
{
352+
if ( left >= right )
353+
{
354+
return;
355+
}
356+
357+
int idx = partition( vec, left, right );
358+
quicksort( vec, left, idx - 1 );
359+
quicksort( vec, idx + 1, right );
360+
}
361+
362+
363+
int main( void )
364+
{
365+
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
366+
printvec( vec );
367+
quicksort( vec, 0, vec.size() - 1 );
368+
printvec( vec, "after insert sort" );
369+
return 0;
370+
}
371+
```
291372
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /