-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_algs.cpp
110 lines (109 loc) · 4.72 KB
/
sort_algs.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
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
108
109
110
#include <iostream>
#include <random>
#include <iomanip>
#include "algorithms/chap2.hpp"
#include "algorithms/chap6.hpp"
int main(int argc, char** argv)
{
bool merge_only = false;
bool insertion_sort = true;
bool merge_sort = false;
bool heap_sort = false;
bool all = false;
std::vector<std::string> all_args;
std::string msg = "Usage: ./sorter [merge | insertion_sort | heap_sort | merge_sort | -help | --h | help]";
msg += "\n- insertion_sort (default setup) runs the insertion sort algorithm on a predefined vector";
msg += "\n- heap_sort runs the heapsort algorithm";
msg += "\n- merge_sort runs the merge sort algorithm on a predefined vector";
msg += "\n- merge runs the merge algorithm on a predefined vector";
msg += "\n- all runs insertion sort and merge sort on a randomly generated vector and times execution for each";
msg += "\n- help | --h | -help displays this message";
if (argc > 1)
{
all_args.assign(argv + 1, argv + argc);
merge_only = all_args[0] == "merge";
insertion_sort = all_args[0] == "insertion_sort";
merge_sort = all_args[0] == "merge_sort";
heap_sort = all_args[0] == "heap_sort";
all = all_args[0] == "all";
}
if(heap_sort)
{
std::vector<float> input_vec{16, 4, 10, 14, 7, 9, 3, 2, 8, 1};
Heap A;
A.vals = input_vec;
A.heap_size = 10;
std::cout << "Before heapify " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
chap6::MaxHeapify(A, 1);
std::cout << "After heapify " << chap2::DisplayVector(A.vals, input_vec.size()) << std::endl;
std::vector<float> input_vec_{4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
Heap B;
B.vals = input_vec_;
B.heap_size = input_vec_.size();
std::cout << "Before building max heap " << chap2::DisplayVector(B.vals, input_vec_.size()) << std::endl;
chap6::BuildMaxHeap(B);
std::cout << "After building max heap " << chap2::DisplayVector(B.vals, input_vec_.size()) << std::endl;
}
else if(insertion_sort)
{
std::vector<float> input_vec{100, 65, 34, 12, 9, -3};
std::vector<float> left, right;
int split_ind = 2;
left.insert(left.begin(), input_vec.begin(), input_vec.begin() + split_ind);
right.insert(right.begin(), input_vec.begin() + split_ind, input_vec.begin() + input_vec.size());
std::cout << "left " << chap2::DisplayVector(left, left.size()) << std::endl;
std::cout << "right " << chap2::DisplayVector(right, right.size()) << std::endl;
try
{
std::cout << "display unsorted vector " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
chap2::InsertionSort(input_vec);
std::cout << "display sorted vector " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
}
else if(merge_only)
{
std::vector<float> input_vec{45, 67, 80, 110, -5, 50, 200};
std::cout << "display unsorted vector " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
chap2::Merge(input_vec, 0, 4, 7);
std::cout << "display sorted vector " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
}
else if(merge_sort)
{
std::vector<float> input_vec{45, 31, 67, 80, -65, 110, -5, 50, 200};
std::cout << "display unsorted vector " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
chap2::MergeSort(input_vec, 0, 7);
std::cout << "display sorted vector " << chap2::DisplayVector(input_vec, input_vec.size()) << std::endl;
}
else if(all)
{
std::vector<float> input_vec;
int vec_size = 10000;
for(int i = 0; i < vec_size; i++)
{
float rand = std::rand() % 1000000;
input_vec.push_back(rand);
}
std::vector<float> first_vector, second_vector;
first_vector = input_vec;
second_vector = input_vec;
std::string msg = "- Insertion sort:\n";
clock_t begin_time = clock();
chap2::InsertionSort(first_vector);
msg += "-runtime " + std::to_string(float(clock() - begin_time) / CLOCKS_PER_SEC) + "s\n";
msg += "- Merge sort:\n";
begin_time = clock();
chap2::MergeSort(second_vector, 0, vec_size);
msg += "-runtime " + std::to_string(float(clock() - begin_time) / CLOCKS_PER_SEC) + "s";
std::cout << std::setprecision(0);
std::cout << msg << std::endl;
}
else
{
std::cout << msg << std::endl;
}
return 0;
}