-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
218 lines (165 loc) · 5.74 KB
/
main.cpp
File metadata and controls
218 lines (165 loc) · 5.74 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
#include <iostream>
#include <fstream>
#include <chrono>
#include <cstdlib>
#include <time.h>
using namespace std::chrono;
using namespace std;
typedef int64_t i64;
// function to handle the display
// not used in the homework assignment
void display(i64 *array, long int size) {
for(long int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
//
void insertionSort(i64 *A, long int size){
i64 key, j;
for(long int i = 1; i<size; i++) {
key = A[i];//take the value
j = i;
while(j > 0 && A[j-1]>key) {
A[j] = A[j-1];
j--;
}
A[j] = key; //insert key in the correct place
}
}
// merges two subarrays L and R into A
// p = starting index
// q = middle index
// r = ending index
void merge(i64 *A, long int p, long int q, long int r) {
// Create copies of the Left and Right portion.
long int nL = q - p + 1;
long int nR = r - q;
i64 L[nL], R[nR];
// Left portion comes from p...q-1
for (long int i = 0; i < nL; i++)
L[i] = A[p + i];
// Right portion comes from q...r-1
for (long int j = 0; j < nR; j++)
R[j] = A[q + 1 + j];
// Initialize the current index of sub-arrays and main array
long int i, j, k;
i = 0; // index for L
j = 0; // index for R
k = p; // index for A from p...r-1
// Until we reach the end of either L or R,
// choose the larger element from L and R
// and copy it to the correct position inside A
while (i < nL && j < nR) {
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
} else {
A[k] = R[j];
j++;
}
k++;
}
// When we are done with elements of either L or R,
// copy the remaining elements to A
// case when R was finished first
while (i < nL) {
A[k] = L[i];
i++;
k++;
}
// case when L was finished first
while (j < nR) {
A[k] = R[j];
j++;
k++;
}
}
// Divide the array into two subarrays, sort them and merge them
// l is the starting index to merge sort
// r is the ending index to merge sort
void mergeSort(i64 *A, long int l, long int r) {
if (l < r) {
// m is the index where we split the original array
long int m = l + (r - l) / 2;
mergeSort(A, l, m);
mergeSort(A, m + 1, r);
// Merge the sorted subarrays
merge(A, l, m, r);
}
}
int main() {
//seed the randomizer.
srand(time(NULL));
fstream input_file;
// Below is the block to choose which file you want to sort in a fast way.
// If you are going to use this portion, you need to comment out the line:
//input_file.open("input.txt",ios::in);
// string filenames[] ={"input_10_random.txt","input_10_sorted.txt","input_10_reverse_sorted.txt",
// "input_1000_random.txt","input_1000_sorted.txt","input_1000_reverse_sorted.txt",
// "input_100000_random.txt","input_100000_sorted.txt","input_100000_reverse_sorted.txt" };
//input_file.open(filenames[7],ios::in);
cout << "Hello!\n";
cout <<"=========================================================================\n";
cout << "Preparing input file.\n";
input_file.open("input.txt",ios::in);
if (!input_file ) {
cout << "File opening problems!";
return 1;
}
long int n; //size of the array, read from the input.txt file below
// read the size from the first line of the file
input_file >> n ;
if (n<=0)
return 1; //there's a problem in the file
cout << "Read n = " << n <<" from input.txt" << endl;
i64 arr_i[n], arr_m[n]; //one for insertion sort, one for merge sort
long int i;
for(i=0; i<n ; i++) {
input_file >> arr_i[i];
arr_m[i] = arr_i[i];
// check for EOF
if (input_file.eof()){
cout << "End of file reached\n";
break;
}
}
if (i!=n){
cout << "Read " << i <<" elements from input.txt, but the n = " << n << ".\nStopping execution!\n" ;
return 1;
}
cout << "Successfully read " << n <<" elements from input.txt" << endl;
input_file.close();
// cout << "Array before sorting: " << endl;
// display(arr_m, n);
// insertion sort testing
auto insertion_start = high_resolution_clock::now();
insertionSort(arr_i, n);
auto insertion_stop = high_resolution_clock::now();
auto insertion_duration_nano = duration_cast<nanoseconds>(insertion_stop - insertion_start);
cout <<"=========================================================================\n";
cout << "It took "<< insertion_duration_nano.count() << " nanoseconds to sort " << n << " elements using insertion sort." << endl;
// merge sort testing
auto merge_start = high_resolution_clock::now();
mergeSort(arr_m, 0, n-1 );
auto merge_stop = high_resolution_clock::now();
auto merge_duration_nano = duration_cast<nanoseconds>(merge_stop - merge_start);
cout << "It took "<< merge_duration_nano.count() << " nanoseconds to sort " << n << " elements using merge sort." << endl;
cout <<"=========================================================================\n";
fstream output_file;
output_file.open("output.txt",ios::out);
if (!output_file) {
cout << "File opening problems!";
return 1;
}
cout << "Preparing output file.\n";
for(long int i=0; i<n ; i++) {
output_file << arr_m[i] <<endl;
}
output_file.close();
cout << "Successfully written output.txt\n";
cout <<"=========================================================================\n";
cout << "Have a nice day ☺\n";
// cout << "Array after Sorting: " << endl;
// display(arr_m, n);
return 0;
}