-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
60 lines (50 loc) · 1.13 KB
/
QuickSort.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
#include <iostream>
#include "QuickSort.h"
using namespace std;
#include <chrono>
//source https://stackoverflow.com/questions/21856025/getting-an-accurate-execution-time-in-c-micro-seconds
//for microseconds
QuickSort::QuickSort()
{
}
QuickSort::~QuickSort()
{
}
void QuickSort::call(double* arr, int size)
{
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
qSort(arr, 0, size-1);
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
auto time = chrono::duration_cast<chrono::microseconds>(t2-t1).count();
cout << "QuickSort took " << time << " microseconds." <<endl;
}
void QuickSort::qSort(double* arr, int low, int high)
{
if(low < high)
{
int pivot = partition(arr, low, high);
qSort(arr, low, pivot-1);
qSort(arr, pivot+1, high);
}
}
int QuickSort::partition(double* arr, int low, int high)
{
double temp;
double pivot = arr[high];
int n = low - 1;
for(int i = low; i <= high-1; i++)
{
if(arr[i] <= pivot)
{
n++;
temp = arr[n];
arr[n] = arr[i];
arr[i] = temp;
}
}
n++;
temp = arr[n];
arr[n] = arr[high];
arr[high] = temp;
return n;
}