This repository was archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorting.h
173 lines (146 loc) · 4.56 KB
/
sorting.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Title: Algorithm Efficiency and Sorting
* Author: Zeynep Cankara
* ID: 21703381
* Section: 2
* Assignment: 1
* Description: Header file for the sorting algorithms + performance analysis
*/
#ifndef sorting_h
#define sorting_h
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
#include <time.h> // for timer
#include <stdlib.h> //rand
#include <iomanip>
using namespace std;
/**
* @brief Sorts an array in ascending order
* Idea: array divided into (unsorted | sorted) subregions. Largest element bubble up with every iteration
* Worst case: O(n^2)
* Best case: O(n) where array "sorted" order
* Average case: O(n^2)
*/
void bubbleSort(int *arr, int size, int &compCount, int &moveCount);
/**
* @brief Sorts an array in ascending order
* Idea: array divided into (<pivot | >pivot) subregions around chosen pivot.
* Worst case: O(n^2)
* Best case: O(n*log(n)) efficient pivot selection
* Average case: O(n*log(n))
* @param arr array to be sorted
* @param size size of the array
* @param compCount number of comparisons between array element
* @param moveCount number of data moves
*/
void quickSort(int *arr, int size, int &compCount, int &moveCount);
/**
* @brief Sorts an array in ascending order
* Idea: Divide array in half. Sort subarrays. Merge subarrays
* Worst case: O(n*log(n))
* Best case: O(n*log(n)) Independent of the array sorted or not
* Average case: O(n*log(n)) Independent of the array sorted or not
* @param arr array to be sorted
* @param size size of the array
* @param compCount number of comparisons between array element
* @param moveCount number of data moves
*/
void mergeSort(int *arr, int size, int &compCount, int &moveCount);
/**
* Find the number in the array with the highest number of digits
* Create 10 container (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
* Starting from the least sig digit group the numbers within the containers
*/
void radixSort(int *arr, int size);
/**
* @brief Prints contents of the array
* @param arr array to printed
* @param size of the arrat
*/
void printArray(int *arr, int size);
/**
* @brief Analyzes performance of algorithms and comparison counts togather with data movements
*/
void performanceAnalysis();
// * * * * HELPER FUNCTIONS * * * * * * * * * *
// MERGE SORT HELPERS
/**
* @brief Helper merge function
* @param arr integer array
* @param start begin index (inclusive)
* @param mid middle index
* @param end last index (inclusive)
* @param compCount number of comparisons between array element
* @param moveCount number of data moves
*/
void merge(int *arr, int start, int mid, int end, int &compCount, int &moveCount);
/**
* @brief Merge Sort algorithm
* @param arr integer array
* @param start begin index (inclusive)
* @param end last index (exclusive)
* @param compCount number of comparisons between array element
* @param moveCount number of data moves
*/
void mergeSort(int *arr, int start, int end, int &compCount, int &moveCount);
// QUICK SORT HELPERS
/**
* @brief Quick Sort algorithm
* @param arr integer array
* @param start begin index (inclusive)
* @param end last index (inclusive)
* @param compCount number of comparisons between array element
* @param moveCount number of data moves
*/
void quickSort(int *arr, int start, int end, int &compCount, int &moveCount);
/**
* @brief Helper partition function
* @param arr integer array
* @param start begin index (inclusive)
* @param end last index (inclusive)
*/
void partition(int *arr, int start, int end, int &pivotIdx, int &compCount, int &moveCount);
/**
* @brief Helper SWAP
*/
void swap(int &a, int &b);
// RADIX SORT HELPERS
/**
* @brief Radix Sort helper
* @param number type(int)
* @return numDigit number of digits of the number
*/
int numDigits(int number);
/**
* @brief Get the max element's num digits within the array
* @param arr array to get max element
* @param n size of the array
*/
int getMaxItemDigit(int *arr, int n);
/**
* @brief Radix Sort helper
* @param arr array
* @param n size of the array
* @param numDigits number of digits of the items
*/
void radixSort(int *arr, int n, int numDigits);
// Performance Analysis helpers
/**
* @brief calculates time elapsed
*/
string printPerformanceOut(int * arr, int size, string algoType);
/**
* @brief Obtain result for randomly generated arrays
*/
void getResult(int size, string *resArr);
/**
* @brief Prints header for the algorithm of interest
*/
void printHeader(string algoType);
/**
* @brief create random arrays
*/
void createArrays(int *arr1, int *arr2, int *arr3, int *arr4, int size);
#endif