-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSection_1_Array_Sorting.cpp
More file actions
625 lines (494 loc) · 22.4 KB
/
Copy pathSection_1_Array_Sorting.cpp
File metadata and controls
625 lines (494 loc) · 22.4 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*****************************************************************************************************************************
Module Name: White-box texting of sine and cosine of numbers
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Description:
- This program determines the effects of size and data order of the array by measuring the time taken when
array sort operations are performed on an array of integers.
- It also tests ten arrays each time, when array is in random order, increasing order and reverse order with
three different array sizes i.e. n=100000, 1000000, 10000000
- It prints the time taken by the arrays in a well-formatted console output
*****************************************************************************************************************************/
///algorithms library defines functions for searching, sorting, counting, manipulating, etc. that operate on ranges of elements
#include <algorithm>
///chrono library deals with time, by means of clocks, time points, durations
#include <chrono>
/// Library for input output capabilities
#include <iostream>
///to use vectors, that is a sequence container that encapsulates dynamic size arrays
#include <vector>
/// tells the compiler to make all the names in the predefined standard library available to our program
using namespace std;
/// to use the time interval - All the elements are not defined directly under the std namespace, but under the std::chrono namespace
using namespace std::chrono;
/// values array for storing random values & i is for loop iterations
long int values[10000000], i;
/*****************************************************************************************************************************
Module Name: Function to print the starting point of sort testing of Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function to notify the start of testing of Array
@param n The array size to be tested
@return null
*****************************************************************************************************************************/
void start_test(int n)
{
cout << "*****Sort Testing for " << n << " array size STARTS NOW*****" << endl;
}
/*****************************************************************************************************************************
Module Name: Function to print the ending point of sort testing of Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function to notify the end of testing of Array
@param n The array size to be tested
@return null
*****************************************************************************************************************************/
void stop_test(int n)
{
cout << "*****Sort Testing for " << n << " array size ENDS NOW*****" << endl;
cout << endl;
}
/*****************************************************************************************************************************
Module Name: Function to print lines
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function to print a line for well formatted output in tabular form
@param none
@return null
*****************************************************************************************************************************/
void line()
{
cout << "--------------------------------------------------------------------------------------------------------------------------------------------\n"
<< endl;
}
/*****************************************************************************************************************************
Module Name: Function for 100,000 random elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 100,000 random elements in an Array
@param none
@return the duration for generating 100,000 random elements in an Array
*****************************************************************************************************************************/
int hundred_thousand_random()
{
/// Initialize a vector of 100000 elements
vector<int> values(100000);
/// Generate random values
auto f = []() -> int
{ return rand() % 100000; };
/// Fill up the vector
generate(values.begin(), values.end(), f);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the function, here sort()
sort(values.begin(), values.end());
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 100,000 ordered elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 100,000 ordered elements in an Array
@param none
@return the duration for sorting 100,000 elements in an Array
*****************************************************************************************************************************/
int hundred_thousand_ordered()
{
/// for loop for storing values in array
for (i = 1; i <= 100000; i++)
{
values[i] = i;
}
/// Get number of elements in values[] array
int n = sizeof(values) / sizeof(values[0]);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the sort function here
sort(values, values + n);
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 100,000 reverse elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 100,000 reverse elements in an Array
@param none
@return the duration for 100,000 reverse elements in an Array
*****************************************************************************************************************************/
int hundred_thousand_reverse()
{
/// for loop for reversing values in array
for (i = 100000; i >= 1; i--)
{
values[i] = i;
}
/// Get number of elements in values[] array
int n = sizeof(values) / sizeof(values[0]);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the function sort() here
sort(values, values + n);
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 1,000,000 random elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 1,000,000 random elements in an Array
@param none
@return the duration for 1,000,000 random elements in an Array
*****************************************************************************************************************************/
int million_elements_random_array()
{
/// Initialize a vector of 1000000 elements
vector<int> values(1000000);
/// Generate random values
auto f = []() -> int
{ return rand() % 1000000; };
/// Fill up the vector
generate(values.begin(), values.end(), f);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the function sort() here
sort(values.begin(), values.end());
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 1,000,000 ordered elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 1,000,000 ordered elements in an Array
@param none
@return integer data type that stores the duration for 1,000,000 ordered elements in an Array
*****************************************************************************************************************************/
int million_elements_ordered_array()
{
/// for loop for storing values in array
for (i = 1; i <= 1000000; i++)
{
values[i] = i;
}
/// Get number of elements in values[] array
int n = sizeof(values) / sizeof(values[0]);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the sort() function here
sort(values, values + n);
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer data type
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 1,000,000 reverse elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 1,000,000 reverse elements in an Array
@param none
@return integer data type that stores the duration for 1,000,000 reverse elements in an Array
*****************************************************************************************************************************/
int million_elements_reverse_array()
{
/// for loop for storing 1000000 reverse elements in array
for (i = 1000000; i >= 1; i--)
{
values[i] = i;
}
/// Get number of elements in values[] array
int n = sizeof(values) / sizeof(values[0]);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the function, here sort()
sort(values, values + n);
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer data type
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 1,000,000 random elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 1,000,000 random elements in an Array
@param none
@return integer data type that stores the duration for 1,000,000 random elements in an Array
*****************************************************************************************************************************/
int tenmillion_elements_random_array()
{
/// Initialize a vector of 10000000 elements
vector<int> values(10000000);
/// generate random values
auto f = []() -> int
{ return rand() % 10000000; };
/// Fill up the vector
generate(values.begin(), values.end(), f);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the function, here sort()
sort(values.begin(), values.end());
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer data type
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 10,000,000 ordered elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 10,000,000 ordered elements in an Array
@param none
@return integer data type that stores the duration for 10,000,000 ordered elements in an Array
*****************************************************************************************************************************/
int ten_million_elements_ordered_array()
{
/// for loop for storing 10000000 ordered elements in array
for (i = 1; i <= 10000000; i++)
{
values[i] = i;
}
/// Get number of elements in values[] array
int n = sizeof(values) / sizeof(values[0]);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the sort() function here
sort(values, values + n);
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer data type
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Function for 10,000,000 reverse elements in an Array
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function for 10,000,000 reverse elements in an Array
@param none
@return integer data type that stores the duration for 10,000,000 reverse elements in an Array
*****************************************************************************************************************************/
int ten_million_elements_reverse_array()
{
/// for loop for storing 10000000 reverse elements in array
for (i = 10000000; i >= 1; i--)
{
values[i] = i;
}
/// Get number of elements in values[] array
int n = sizeof(values) / sizeof(values[0]);
/// Get starting timepoint
auto start = high_resolution_clock::now();
/// Call the sort() function here
sort(values, values + n);
/// Get ending timepoint
auto stop = high_resolution_clock::now();
/// Get the duration by subtracting stop time from start time
auto duration = duration_cast<milliseconds>(stop - start);
/// Return the time taken in integer data type
return duration.count();
}
/*****************************************************************************************************************************
Module Name: Main Function
Author: Faiza Fatma Siddiqui
Date Created/Modified:17.10.2021
Student ID: 200473896
Purpose: CS 700 - Software Development Fundamentals - Assignment 2
Function to print time durations of every sort performed on arrays of different sizes
@param none
@return 0 is the return value, if no error & non-zero if there is error
*****************************************************************************************************************************/
int main()
{
/// Arrays for storing 10 time durations of hundred thousand elements in random, ordered and reverse form
int hundred_random[10], hundred_ordered[10], hundred_reverse[10];
/// Arrays for storing 10 time durations of million elements in random, ordered and reverse form
int million_random[10], million_ordered[10], million_reverse[10];
/// Arrays for storing 10 time durations of ten million elements in random, ordered and reverse form
int ten_million_random[10], ten_million_ordered[10], ten_million_reverse[10];
/// Start the test for 100000 elements
start_test(100000);
/// for loop for storing 10 time durations of hundred thousand elements in random, ordered and reverse form
for (int i = 1; i <= 10; i++)
{
/// Storing 10 time durations of hundred thousand elements in random order
hundred_random[i] = hundred_thousand_random();
/// Storing 10 time durations of hundred thousand elements in ordered way
hundred_ordered[i] = hundred_thousand_ordered();
/// Storing 10 time durations of hundred thousand elements in reverse order
hundred_reverse[i] = hundred_thousand_reverse();
}
/// Stop the test for 100000 elements
stop_test(100000);
/// Start the test for 1000000 elements
start_test(1000000);
/// for loop for storing 10 time durations of million elements in random, ordered and reverse form
for (int i = 1; i <= 10; i++)
{
/// Storing 10 time durations of million elements in random order
million_random[i] = million_elements_random_array();
/// Storing 10 time durations of million elements in ordered way
million_ordered[i] = million_elements_ordered_array();
/// Storing 10 time durations of million elements in reverse order
million_reverse[i] = million_elements_reverse_array();
}
/// Stop the test for million elements
stop_test(1000000);
/// Start the test for 10000000 elements
start_test(10000000);
/// for loop for storing 10 time durations of ten million elements in random, ordered and reverse form
for (int i = 1; i <= 10; i++)
{
/// Storing 10 time durations of million elements in random order
ten_million_random[i] = tenmillion_elements_random_array();
/// Storing 10 time durations of million elements in ordered way
ten_million_ordered[i] = ten_million_elements_ordered_array();
/// Storing 10 time durations of million elements in reverse order
ten_million_reverse[i] = ten_million_elements_reverse_array();
}
/// Stop the test for ten million elements
stop_test(10000000);
/// printing heading for well-formatted console output
cout << "============== Printing Sort Duration in milliseconds for 10 Arrays of size 100,000 each ==============" << endl;
/// printing a line for well-formatted console output
line();
/// Printing the name of test, i.e. the order of the Array - Random
cout << "|RANDOM \t|";
/// for printing time duration of Arrays of size 100,000 in random order
for (int i = 1; i <= 10; i++)
{
cout << hundred_random[i] << "ms\t|";
}
cout << endl;
/// Printing the name of test, i.e. the order of the Array - Ordered
cout << "|ORDERED\t|";
/// for printing time duration of Arrays of size 100,000 in ordered way
for (int i = 1; i <= 10; i++)
{
cout << hundred_ordered[i] << "ms\t|";
}
cout << endl;
/// Printing the name of test, i.e. the order of the Array - Reverse
cout << "|REVERSE\t|";
/// for printing time duration of Arrays of size 100,000 in reverse way
for (int i = 1; i <= 10; i++)
{
cout << hundred_reverse[i] << "ms\t|";
}
cout << endl;
/// printing a line for well-formatted console output
line();
/// printing heading for well-formatted console output
cout << "============= Printing Duration in milliseconds for 10 Arrays of size 1,000,000 each ==============" << endl;
/// printing a line for well-formatted console output
line();
/// printing name of Array order test for well-formatted console output
cout << "|RANDOM \t|";
/// for printing time duration of 10 Arrays of size 1,000,000 in random order
for (int i = 1; i <= 10; i++)
{
cout << million_random[i] << "ms\t|";
}
cout << endl;
/// printing name of Array order test for well-formatted console output
cout << "|ORDERED\t|";
/// for printing time duration of 10 Arrays of size 1,000,000 in ordered way
for (int i = 1; i <= 10; i++)
{
cout << million_ordered[i] << "ms\t|";
}
cout << endl;
/// printing name of Array order test for well-formatted console output
cout << "|REVERSE\t|";
/// for printing time duration of 10 Arrays of size 1,000,000 in reverse order
for (int i = 1; i <= 10; i++)
{
cout << million_reverse[i] << "ms\t|";
}
cout << endl;
/// printing a line for well-formatted console output
line();
/// printing heading for well-formatted console output
cout << "============== Printing Duration in milliseconds for 10 Arrays of size 10,000,000 each ==============" << endl;
/// printing a line for well-formatted console output
line();
/// printing name of Array order test for well-formatted console output
cout << "|RANDOM \t|";
/// for printing time duration of 10 Arrays of size 10,000,000 in random order
for (int i = 1; i <= 10; i++)
{
cout << ten_million_random[i] << "ms\t|";
}
cout << endl;
/// printing name of Array order test for well-formatted console output
cout << "|ORDERED\t|";
/// for printing time duration of 10 Arrays of size 10,000,000 in ordered way
for (int i = 1; i <= 10; i++)
{
cout << ten_million_ordered[i] << "ms\t|";
}
cout << endl;
/// printing name of Array order test for well-formatted console output
cout << "|REVERSE\t|";
/// for printing time duration of 10 Arrays of size 10,000,000 in reverse order
for (int i = 1; i <= 10; i++)
{
cout << ten_million_reverse[i] << "ms\t|";
}
cout << endl;
/// printing a line for well-formatted console output
line();
///return 0 if program executed successfully because main function is of type integer
return 0;
}