-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path000quickSort.cpp
More file actions
56 lines (46 loc) · 1.01 KB
/
000quickSort.cpp
File metadata and controls
56 lines (46 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by Pasco on 16/4/19.
//
//
#include "catch.hpp"
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void quickSort(vector<int>& arr, int left, int right)
{
if (left >= right)
{
return;
}
int base = left;
int i = left;
int j = right;
while (i != j)
{
while (arr[j] < arr[base] && i < j)
{
j--;
}
while (arr[i] > arr[base] && i < j)
{
i++;
}
if (i != j)
{
swap(arr[i], arr[j]);
}
}
swap(arr[base],arr[i]);
quickSort(arr, left, i);
quickSort(arr, i+1,right);
}
};
TEST_CASE("000quickSort") {
Solution solution;
vector<int > fooArray = {5, 2, 6, 3, 8};
vector<int > expectOutput = {8, 6, 5, 3, 2};
solution.quickSort(fooArray, 0, fooArray.size()-1);
REQUIRE(expectOutput == fooArray);
}