-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksortcpp.cpp
71 lines (62 loc) · 1.54 KB
/
quicksortcpp.cpp
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
70
71
#include <iostream>
#include <vector>
#include <random>
int partition(std::vector<int> &arr, int low, int high){
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(low,high);
int pivot = dist(rng);
//this swaps the random pivot with the last element
int temp = arr[pivot];
arr[pivot] = arr[high];
arr[high] = temp;
int i = low;
int j = high - 1;
while( i <= j)
{
if(arr[i] < arr[high])
{
if( arr[j] > arr[high])
{
//swap values at array i and j
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
else
{
j--; //decrement until I find a j that is larger than our selected pivot
}
}
else
{
i++; //incremenet until I ifnd a i that is smaller than out selected pivot
}
}
temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i;
}
void quicksort(std::vector<int> &arr, int low, int high)
{
if(low < high)
{
int pivot = partition(arr, low, high);
quicksort(arr, low, pivot-1);
quicksort(arr, pivot+1, high);
}
}
int main()
{
std::vector<int> arr = {4,1,2,6,8,10,31};
quicksort(arr,0,arr.size()-1);
for(int i = 0; i < arr.size(); i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}