-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorter.cpp
115 lines (107 loc) · 3.59 KB
/
Sorter.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
111
112
113
114
115
// Source file for Sorter
// Created by Michael Gosling on 2/18/19.
//
#include <iostream>
#include <fstream>
#include "Sorter.h"
#include <cstring>
#include <iterator>
#include "SortingAlgos.h"
/**
* Fills an array with random unsorted values
* @param array Array to fill
*/
void Sorter::createUnsortedArray(int array[]) {
// seed rand
srand((unsigned)time(nullptr));
for (auto i = 0; i < arraySize; i++)
array[i] = rand() % 32767; // random num between 0 and 32767
}
/**
* Write an array to a file
* @param array Array to write
* @param fileName Filename to write to
*/
void Sorter::writeArrToFile(int array[], std::string fileName) {
std::ofstream outputFile; // output file
outputFile.open(fileName); // open file name
// if the file is open
if (outputFile.is_open()){
// write each integer in the array to the file followed by a comma, unless it's the last
for (auto i = 0; i < arraySize; i++){
outputFile << array[i];
if (i != arraySize-1) outputFile << ",";
}
outputFile.close(); // close file
} else
std::cout << "Failed to open file" << std::endl; // display console message if file isn't not open
}
/**
* Reset the sorting array to the initial unsorted values
*/
void Sorter::resetSortingArray(){
for (auto i = 0; i < arraySize; i++)
sortingArray[i] = unsortedArray[i];
}
/**
* Run the given sorting algorithm
* @param algo Algorithm to use
*/
void Sorter::runSort(Algo algo) {
resetSortingArray(); // reset sorting array to unsorted state
std::string fileName; // output filename
start = std::clock(); // start time
// depending on the given algorithm, set the file name and run the corresponding sort algo
switch (algo) {
case Bubble:
fileName = "BubbleSort.txt";
SortingAlgos::bubbleSort(sortingArray, arraySize);
break;
case Selection:
fileName = "SelectionSort.txt";
SortingAlgos::selectionSort(sortingArray, arraySize);
break;
case Insertion:
fileName = "InsertionSort.txt";
SortingAlgos::insertionSort(sortingArray, arraySize);
break;
case Shell:
fileName = "ShellSort.txt";
SortingAlgos::shellSort(sortingArray, arraySize);
break;
case Quick:
fileName = "QuickSort.txt";
SortingAlgos::quickSort(sortingArray, arraySize);
break;
case Merge:
fileName = "MergeSort.txt";
SortingAlgos::mergeSort(sortingArray, 0, arraySize-1);
break;
case Radix:
fileName = "RadixSort.txt";
SortingAlgos::radixSort(sortingArray, arraySize);
break;
}
end = std::clock(); // end time
duration = (end - start) / (double) CLOCKS_PER_SEC; // duration
// if the array is 1000 items long, just write it to file and tell the user it's written.
// otherwise, display the timing values
if (arraySize == 1000) {
writeArrToFile(sortingArray, fileName);
std::cout << "Written to file!" << std::endl << std::endl;
} else
std::cout << "Start: " << start << std::endl << "End: " << end << std::endl <<
"Duration: " << duration << "s" << std::endl << std::endl;
}
/**
* Constructor
* @param size Size of the array
*/
Sorter::Sorter(int size) {
arraySize = size;
// create unsorted array
unsortedArray = new int[arraySize];
createUnsortedArray(unsortedArray);
// create sorting array of same size
sortingArray = new int[arraySize];
}