Repository navigation
Expand file tree
/
Copy pathquicksort.cpp
More file actions
executable file
·107 lines (89 loc) · 2.93 KB
/
Copy pathquicksort.cpp
File metadata and controls
executable file
·107 lines (89 loc) · 2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// quicksort.cpp
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <chrono>
// Helper functions
void test_randomize(int *, int);
void randomize(int *, int);
void print_array(int *, int);
void swap_elements(int *, int, int);
// Main functions
int hoare_partition(int *, int, int);
void quicksort(int *, int, int);
//We will be taking two command line arguments.
//The first command line argument is the array size (N) to work with.
//The second command line argument is the name of the file to append the results to.
int main(int argc, char * argv[]) {
const int SIZE = atoi(argv[1]);
int * arr = new int[SIZE];
std::ofstream myfile;
randomize(arr, SIZE);
std::cout << "BEGIN QUICKSORT" << std::endl;
std::cout << "ARRAY SIZE: " << SIZE << std::endl;
//Start the timer
auto t_start = std::chrono::high_resolution_clock::now();
quicksort(arr, 0, SIZE-1);
std::cout << "FINISHED QUICKSORT" << std::endl;
//End the timer
auto t_end = std::chrono::high_resolution_clock::now();
// UNCOMMENT ONLY FOR TESTING. OTHERWISE NOT RECOMMENDED TO PRINT VALUES WHERE ARRAY SIZE IS LARGE.
//print_array(arr, SIZE);
std::cout << "ELAPSED TIME: "
<< std::chrono::duration<double, std::milli>(t_end-t_start).count()
<< " ms\n";
myfile.open(argv[2], std::fstream::out | std::fstream::app);
myfile << std::chrono::duration<double, std::milli>(t_end-t_start).count() << '\n';
return 0;
}
// The quicksort implementation as described by Levitin.
// INPUT: array of integers and the INDEX positions of the leftmost and rightmost elements
// OUTPUT: a sorted array thanks to quicksort
void quicksort(int * arr, int l, int r) {
if(l < r) {
//S is our split position retrieved from hoare partinioning our array
int s = hoare_partition(arr, l, r);
//Run quicksort on the left and right side of the split positions
quicksort(arr, l, s-1);
quicksort(arr, s+1, r);
}
}
// Helper function hoare partition will partition our subarray
// and return the split position. It will also ensure that left and
// right values of the split position is smaller and larger respectively.
int hoare_partition(int * arr, int l, int r) {
int p, i, j;
p = arr[l];
i = l;
j = r + 1;
while(i < j) {
do {
++i;
} while(arr[i] < p);
do {
--j;
} while(arr[j] > p);
swap_elements(arr, i, j);
}
swap_elements(arr, i, j);
swap_elements(arr, l, j);
return j;
}
// Utility function to fill array with random integers
void randomize(int * arr, int length) {
for(int i = 0; i < length; ++i) {
arr[i] = rand() % length;
}
}
// Utility function to swap two elements in an array of integers
void swap_elements(int * arr, int indexA, int indexB) {
int temp = arr[indexA];
arr[indexA] = arr[indexB];
arr[indexB] = temp;
}
// Utility function to print an array
void print_array(int * arr, int length) {
for(int i = 0; i < length; ++i) {
std::cout << arr[i] << std::endl;
}
}