-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.h
126 lines (103 loc) · 3.25 KB
/
base.h
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Operator for printing vectors.
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
int n = v.size();
os << "{";
for (int i = 0; i < v.size(); i++){
os << v[i];
if (i < n - 1){
os << ", ";
}
}
os << "}";
return os;
}
/*
SortStats stores statistics about one run of a sorting routine.
"ulong" is used to shorten "unsigned long"
*/
typedef unsigned long ulong;
struct SortStats{
string sort_name;
size_t vector_size = 0;
ulong num_comparisons = 0;
double cpu_running_time_sec = 0.0;
string to_csv() const{
return sort_name + ", " + to_string(vector_size) + ", " + to_string(num_comparisons) + ", " + to_string(cpu_running_time_sec);
}
};
// Operator for printing SortStats objects.
ostream &operator<<(ostream &os, const SortStats &ss){
os << "SortStats{" << ss.sort_name
<< ", size=" << ss.vector_size
<< ", num_comparisons=" << ss.num_comparisons
<< ", cpu_running_time_sec=" << ss.cpu_running_time_sec
<< "}";
return os;
}
/*
Returns true if v[0] <= v[1] <= ... <= v[n-1], and false otherwise.
Returns true if v is empty, i.e. an empty vector is considered sorted.
*/
template <typename T>
bool is_sorted(vector<T> &v);
/*
Sorting algorithm headers. Each algorithm implements the named algorithm, and
returns a SortStats object containing statistics about the sorting run.
*/
/*
Bubble Sort: Compares and swaps adjacent elements until the entire array is sorted.
Inefficient for large datasets.
Quadratic time complexity O(n^2).
*/
template <typename T>
SortStats bubble_sort(vector<T> &v);
/*
Insertion Sort: Builds the final sorted array one item at a time.
Efficient for small datasets or nearly sorted data.
Quadratic time complexity O(n^2) in the worst case.
*/
template <typename T>
SortStats insertion_sort(vector<T> &v);
/*
Selection Sort: Finds the minimum element from the unsorted part and swaps it with the first element.
Repeats until the entire array is sorted.
Quadratic time complexity O(n^2).
*/
template <typename T>
SortStats selection_sort(vector<T> &v);
/*
Merge Sort: Divides the array into two halves, recursively sorts them, and then merges them.
Guaranteed O(n log n) time complexity.
Requires additional space.
*/
template <typename T>
SortStats merge_sort(vector<T> &v);
/*
Quick Sort: Chooses a pivot, partitions the array into two halves,
and recursively sorts each half.
Average-case O(n log n) time complexity.
*/
template <typename T>
SortStats quick_sort(vector<T> &v);
/*
Shell Sort: An extension of insertion sort that allows for swapping elements
that are far apart. Reduces the distance between elements gradually.
Time complexity varies but often better than O(n^2).
*/
template <typename T>
SortStats shell_sort(vector<T> &v);
/*
Improved Quick Sort (Iquick Sort): An optimized version of Quick Sort that switches
to Insertion Sort for small subarrays to reduce overhead.
Average-case O(n log n) time complexity.
*/
template <typename T>
SortStats iquick_sort(vector<T> &v);
// Returns a vector of n randomly chosen ints, each <= max and >= min.
vector<int> rand_vec(int n, int min, int max);