Skip to content

Commit f596a25

Browse files
committed
feat: update quickSort in cpp
1 parent f5bb05b commit f596a25

File tree

5 files changed

+49
-148
lines changed

5 files changed

+49
-148
lines changed

basic/sorting/QuickSort/Main.cpp

+22-70
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,32 @@
11
#include <iostream>
2-
#include <vector>
32

43
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-
}
434

44-
if ( left >= right )
45-
{
46-
break;
47-
}
5+
const int N = 1e6 + 10;
486

49-
vec[right] = vec[left];
50-
}
7+
int n;
8+
int nums[N];
519

52-
vec[left] = base;
53-
return left;
54-
}
55-
56-
57-
void quicksort( vector<int> & vec, int left, int right )
10+
void quick_sort(int nums[], int left, int right)
5811
{
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 );
12+
if (left >= right) return;
13+
int i = left - 1, j = right + 1;
14+
int x = nums[left + right >> 1];
15+
while (i < j)
16+
{
17+
while (nums[++i] < x);
18+
while (nums[--j] > x);
19+
if (i < j) swap(nums[i], nums[j]);
20+
}
21+
quick_sort(nums, left, j);
22+
quick_sort(nums, j + 1, right);
6723
}
6824

69-
70-
int main( void )
25+
int main()
7126
{
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-
27+
int n;
28+
scanf("%d", &n);
29+
for (int i = 0; i < n; ++i) scanf("%d", &nums[i]);
30+
quick_sort(nums, 0, n - 1);
31+
for (int i = 0; i < n; ++i) printf("%d ", nums[i]);
32+
}

basic/sorting/QuickSort/Main.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static void main(String[] args) {
1010
}
1111
quickSort(nums, 0, n - 1);
1212
for (int i = 0; i < n; ++i) {
13-
System.out.printf("%d ", nums[i]);
13+
System.out.print(nums[i] + " ");
1414
}
1515
}
1616

@@ -19,7 +19,7 @@ public static void quickSort(int[] nums, int left, int right) {
1919
return;
2020
}
2121
int i = left - 1, j = right + 1;
22-
int x = nums[left];
22+
int x = nums[(left + right) >> 1];
2323
while (i < j) {
2424
while (nums[++i] < x);
2525
while (nums[--j] > x);

basic/sorting/QuickSort/Main.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ function quickSort(nums, left, right) {
2424
while (nums[++i] < x);
2525
while (nums[--j] > x);
2626
if (i < j) {
27-
const t = nums[i];
28-
nums[i] = nums[j];
29-
nums[j] = t;
27+
[nums[i], nums[j]] = [nums[j], nums[i]];
3028
}
3129
}
3230
quickSort(nums, left, j);

basic/sorting/QuickSort/README.md

+24-69
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ public class Main {
110110
}
111111
quickSort(nums, 0, n - 1);
112112
for (int i = 0; i < n; ++i) {
113-
System.out.printf("%d ", nums[i]);
113+
System.out.print(nums[i] + " ");
114114
}
115115
}
116-
116+
117117
public static void quickSort(int[] nums, int left, int right) {
118118
if (left >= right) {
119119
return;
120120
}
121121
int i = left - 1, j = right + 1;
122-
int x = nums[left];
122+
int x = nums[(left + right) >> 1];
123123
while (i < j) {
124124
while (nums[++i] < x);
125125
while (nums[--j] > x);
@@ -291,81 +291,36 @@ fn main() -> io::Result<()> {
291291

292292
```cpp
293293
#include <iostream>
294-
#include <vector>
295294

296295
using namespace std;
297-
void printvec( const vector<int> &vec, const string &strbegin = "", const string &strend = "" )
298-
{
299-
cout << strbegin << endl;
300-
for ( auto val : vec )
301-
{
302-
cout << val << "\t";
303-
}
304296

305-
cout << endl;
306-
cout << strend << endl;
307-
}
297+
const int N = 1e6 + 10;
308298

299+
int n;
300+
int nums[N];
309301

310-
int partition( vector<int> & vec, int left, int right )
302+
void quick_sort(int nums[], int left, int right)
311303
{
312-
if ( left >= right )
313-
{
314-
return left;
315-
}
316-
317-
int base = vec[left];
318-
while ( left < right )
319-
{
320-
while ( left < right && vec[right] >= base )
321-
{
322-
right--;
323-
}
324-
if ( left >= right )
325-
{
326-
break;
327-
}
328-
329-
vec[left] = vec[right];
330-
331-
while ( left < right && vec[left] <= base )
332-
{
333-
left++;
334-
}
335-
336-
if ( left >= right )
337-
{
338-
break;
339-
}
340-
341-
vec[right] = vec[left];
342-
}
343-
344-
vec[left] = base;
345-
return left;
346-
}
347-
348-
349-
void quicksort( vector<int> & vec, int left, int right )
350-
{
351-
if ( left >= right )
352-
{
353-
return;
354-
}
355-
356-
int idx = partition( vec, left, right );
357-
quicksort( vec, left, idx - 1 );
358-
quicksort( vec, idx + 1, right );
304+
if (left >= right) return;
305+
int i = left - 1, j = right + 1;
306+
int x = nums[left + right >> 1];
307+
while (i < j)
308+
{
309+
while (nums[++i] < x);
310+
while (nums[--j] > x);
311+
if (i < j) swap(nums[i], nums[j]);
312+
}
313+
quick_sort(nums, left, j);
314+
quick_sort(nums, j + 1, right);
359315
}
360316

361-
362-
int main( void )
317+
int main()
363318
{
364-
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
365-
printvec( vec );
366-
quicksort( vec, 0, vec.size() - 1 );
367-
printvec( vec, "after insert sort" );
368-
return 0;
319+
int n;
320+
scanf("%d", &n);
321+
for (int i = 0; i < n; ++i) scanf("%d", &nums[i]);
322+
quick_sort(nums, 0, n - 1);
323+
for (int i = 0; i < n; ++i) printf("%d ", nums[i]);
369324
}
370325
```
371326

solution/0100-0199/0192.Word Frequency/README_EN.md

-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
<p>For simplicity sake, you may assume:</p>
1010

1111
<ul>
12-
1312
<li><code>words.txt</code> contains only lowercase characters and space <code>&#39; &#39;</code> characters.</li>
14-
1513
<li>Each word must consist of lowercase characters only.</li>
16-
1714
<li>Words are separated by one or more whitespace characters.</li>
18-
1915
</ul>
2016

2117
<p><strong>Example:</strong></p>

0 commit comments

Comments
 (0)