-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProject.cpp
More file actions
255 lines (224 loc) · 8.11 KB
/
Project.cpp
File metadata and controls
255 lines (224 loc) · 8.11 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <ctime>
#define MAXRANGE 1000000
std::string GetArray(const int *ARRAY, const int &N)
{
std::stringstream stream;
std::string stored_value = "";
std::string temp;
for (int i = 0; i < N; i++)
{
stream << ARRAY[i]; // steam <- int
stream >> temp; // steam(int) -> temp
stored_value += " " + temp; // stored_value <- (string)temp(int)
stream.clear(); // clear
}
return stored_value; // return string
}
void Input(int *num, const char var_initial)
{
int num_in = 0; // number for test inputs
while (!(num_in > 0 && num_in <= MAXRANGE)) // repeat if number-input less than 1 and number-input is larger than MAXRANGE
{
printf("Input value for %c: ", var_initial); // prompting message
std::cin >> num_in; // number input
if (num_in < 1 || num_in > MAXRANGE) // check if valid
printf("Invalid value for %c. Please Try Again.\n\n", var_initial); // if true, print a message
}
*num = num_in; // assign the num_in final value to num
}
void RandomizeInitialization(int *ARRAY, const int &N)
{
srand(time(NULL)); // each execusion of the program, will result to a randomize number
for (int i = 0; i < N; i++) // loop to n
ARRAY[i] = (rand() % MAXRANGE); // assign each randomize value to the array
}
void SortedInitialization(int *ARRAY, const int &N, const int &X)
{
for (int i = 0; i < N; i++) // loop to n
ARRAY[i] = N + ((i + 1) * X); // assign each sorted customized values to the array
}
void Copy(int *ORIGINAL_COPY, int *HOLDER, const int &N, const int &option)
{
if (option != 1) // check if the option selected by user is not equal to 1
return;
for (int i = 0; i < N; i++) // if false, then loop to n
HOLDER[i] = ORIGINAL_COPY[i]; // each values of the original array will be assigned to the holder
}
void InsertionSort(int *ARRAY, const int &length)
{
for (int i = 1; i < length; i++) // loop from 1 to n
{
int key = ARRAY[i], j = i; // set key to i and set j to match i
while (j > 0 && ARRAY[j - 1] > key) // while j greater than 0 and array is greater than key - repeat
{
ARRAY[j] = ARRAY[j - 1]; // while true, change value of array[j] to array[j-1]
j--; // decrement j, until it reached 0, for condition to be false
}
ARRAY[j] = key; // assign key to array[j]
}
}
void Merge(int *ARRAY, const int lower, const int middle, const int upper)
{
int i, j, k; // initialize variables to be used
int left[middle - lower + 1]; // create temp array for left side
int right[upper - middle]; // create temp array for right side
for (i = 0; i < middle - lower + 1; i++) // loop from 0 to the middle size of the array
left[i] = ARRAY[lower + i]; // assign the values from the left to middle
for (j = 0; j < upper - middle; j++) // loop from 0 to the middle size of the array
right[j] = ARRAY[middle + 1 + j]; // assign the values from the right to end
i = 0;
j = 0;
k = lower;
for (k = lower; i < middle - lower + 1 && j < upper - middle; k++)
{
if (left[i] <= right[j])
ARRAY[k] = left[i++];
else
ARRAY[k] = right[j++];
}
while (i < middle - lower + 1)
ARRAY[k++] = left[i++];
while (j < upper - middle)
ARRAY[k++] = right[j++];
}
void MergeSort(int *ARRAY, const int lower, const int upper)
{
if (lower >= upper)
return;
MergeSort(ARRAY, lower, (lower + upper) / 2);
MergeSort(ARRAY, ((lower + upper) / 2) + 1, upper);
Merge(ARRAY, lower, (lower + upper) / 2, upper);
}
void Swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
int Partition(int *ARRAY, const int low, const int high)
{
int index = low, pivot = high;
for (int i = low; i < high; i++)
{
if (ARRAY[i] < ARRAY[pivot])
{
Swap(&ARRAY[i], &ARRAY[index]);
index++;
}
}
Swap(&ARRAY[pivot], &ARRAY[index]);
return index;
}
int RandomPivotPartition(int *ARRAY, const int low, const int high)
{
int pvt = low + rand() % (high - low + 1);
Swap(&ARRAY[high], &ARRAY[pvt]);
return Partition(ARRAY, low, high);
}
void QuickSort(int *ARRAY, const int low, const int high)
{
if (low < high)
{
int pivotIndex = RandomPivotPartition(ARRAY, low, high);
QuickSort(ARRAY, low, pivotIndex - 1);
QuickSort(ARRAY, pivotIndex + 1, high);
}
}
void Heapify(int *ARRAY, const int length, const int index)
{
int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < length && ARRAY[left] > ARRAY[largest])
largest = left;
if (right < length && ARRAY[right] > ARRAY[largest])
largest = right;
if (largest != index)
{
Swap(&ARRAY[index], &ARRAY[largest]);
Heapify(ARRAY, length, largest);
}
}
void HeapSort(int *ARRAY, int length)
{
int i;
for (i = length / 2 - 1; i >= 0; i--)
Heapify(ARRAY, length, i);
for (i = length - 1; i >= 0; i--)
{
Swap(&ARRAY[0], &ARRAY[i]);
Heapify(ARRAY, i, 0);
}
}
int main()
{
clock_t start, end;
std::ofstream outputFile;
outputFile.open("record.out");
if (!outputFile.is_open())
{
std::cout << "Error Opening output file" << std::endl;
return -1;
}
// variables, pointer-array initialised with NULL value
int input = 0, N = 0, X = 0, *array = NULL, *temp = NULL;
std::cout << "Welcome to our Project\n\n";
while (input != 1 && input != 2)
{
std::cout << " Choose 1 [To Use Random Initialized Array]" << std::endl;
std::cout << " Choose 2 [To Use Sorted Initialized Array]\n Input: ";
std::cin >> input;
std::cout << std::endl;
}
Input(&N, 'N'); // get N (size)
array = new int[N]; // request memory
if (input == 1)
{
temp = new int[N]; // request memory for temp as N
RandomizeInitialization(array, N);
Copy(array, temp, N, input);
outputFile << "Randomize Initialization\n\t";
}
if (input == 2)
{
temp = new int[0]; // set memory for temp as 0 [unsed and unallocated]
Input(&X, 'X');
SortedInitialization(array, N, X);
outputFile << "Sorted Initialization\n\t";
}
outputFile << "Original Array :" + GetArray(array, N) << std::endl;
// Insertion Sort
start = clock();
InsertionSort(array, N);
end = clock();
outputFile << "\n\tSorted Array :" << GetArray(array, N) << "\n\n\nAnalysis\n";
outputFile << "\tInsertion Sort T(N) = " << ((double)(end - start)) / CLOCKS_PER_SEC << std::endl;
// Merge Sort
Copy(temp, array, N, input);
start = clock();
MergeSort(array, 0, N - 1);
end = clock();
outputFile << "\tMerge Sort T(N) = " << ((double)(end - start)) / CLOCKS_PER_SEC << std::endl;
// Quick Sort
Copy(temp, array, N, input);
start = clock();
QuickSort(array, 0, (N - 1));
end = clock();
outputFile << "\tQuick Sort T(N) = " << ((double)(end - start)) / CLOCKS_PER_SEC << std::endl;
// Heap Sort
Copy(temp, array, N, input);
start = clock();
HeapSort(array, N);
end = clock();
outputFile << "\tHeap Sort T(N) = " << ((double)(end - start)) / CLOCKS_PER_SEC << std::endl;
delete[] array; // Releases memory pointed to by array
delete[] temp; // Releases memory pointed to by array
return 0;
}
// for more information please visit: https://github.com/joemar25/CS111_MidTermProject
// thank you...