-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.cpp
More file actions
69 lines (53 loc) · 1.93 KB
/
p1.cpp
File metadata and controls
69 lines (53 loc) · 1.93 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
57
58
59
60
61
62
63
64
65
66
67
68
69
// Given a vector xs of length n, design an algorithm of choice to sort its integers from
// lowest to highest value. Please avoid using in-built sort methods.
// Assumptions:
// The vector of integers will contain randomly sorted integers
// The vector of integers will fit in memory
#include "p1.h"
/*
partition() returns the index of the pivot
The pivot point will be greater than elements to the right and
less than elements to the left.
*/
int partition(vector <int> &v, int start, int end) {
//the pivot point used will be at end
int pivot = v[end];
//swapIdx will be where elements lower than the pivot value will be put
int swapIdx = start;
//for every element from start to end index
for(int i = start; i < end; i++) {
//if value at i is less than the pivot, then swap v[i] with swapIdx
if(v[i] <= pivot) {
swap(v[swapIdx], v[i]);
//since an element lower than the pivot was swapped, iterate swapIdx
swapIdx++;
}
}
//now the pivot point at end will be swapped with swapIdx
swap(v[end], v[swapIdx]);
/*
After the above swap, swapIdx is now the index of the pivot point.
The value at swapIdx will be greater than elements to the right and
less than elements to the left.
Now this pivot index (swapIdx) is returned
*/
return swapIdx;
}
//quicksort() is called recursively
void quicksort(vector <int> &v, int start, int end) {
if(end <= start) {
return;
}
/*
all elements in the vector are partitioned to the left and right of
the pivot point returned by partition.
*/
int pivot = partition(v, start, end);
quicksort(v, start, pivot - 1);
quicksort(v, pivot + 1, end);
}
//sorting method used is quicksort
//quicksort will sort the given vector in place
void quicksort(vector <int> &v) {
quicksort(v, 0, v.size()-1);
}