Skip to content

Commit 448bcbb

Browse files
⚡IMPROVE and Commented
1 parent 1f801d8 commit 448bcbb

File tree

2 files changed

+67
-41
lines changed

2 files changed

+67
-41
lines changed

Insertion-Sort.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,79 @@
1+
2+
// Libraries to be added
3+
14
#include <iostream>
25
#include <cstdlib>
36
#include <iomanip>
7+
#include <chrono>
48
#include <time.h>
59
#include <fstream>
610
using namespace std;
11+
12+
// Function Prototypes
13+
714
void InsertionSort(int array[], int start, int end);
815
void RandomArray(int array[], int size);
9-
int main() {
16+
17+
int main()
18+
{
1019
int size;
1120
cout << "Enter the Size of Array: ";
1221
cin >> size;
13-
int* A = new int[size];
22+
int *A = new int[size];
23+
24+
// To generate random Numbers and store them
25+
1426
srand(time(NULL));
1527
RandomArray(A, size);
1628

17-
/*cout << "\n Numbers before Sorting\n to print them, ";
18-
system("pause");
19-
for (int i = 0; i < size; i++)
20-
cout << A[i] << endl;
21-
*/
22-
clock_t start, end;
23-
start = clock();
29+
// Code to call InsertionSort and measure time duration
30+
31+
auto start = chrono::high_resolution_clock::now();
2432
InsertionSort(A, 0, size - 1);
25-
end = clock();
26-
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
27-
cout << "\n Time taken by the insertion Sort,\nwith " << size << " size of Array is: ";
28-
cout << fixed << time_taken << setprecision(7)<<" seconds"<<endl;
33+
auto end = chrono::high_resolution_clock::now();
34+
double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count();
35+
36+
// Code to calculate the time duration
37+
38+
time_taken *= 1e-9;
39+
cout << "\n Time taken by the insertion Sort, with " << size << " size of Array is: ";
40+
cout << fixed << time_taken << setprecision(9) << " seconds" << endl;
41+
42+
// Code to Save sorted array in csv file
2943

3044
ofstream OutData;
3145
OutData.open("SortedInsertionSort.csv");
3246
for (int i = 0; i < size; i++)
3347
OutData << A[i] << endl;
34-
/*cout << "\n\n Numbers after Sorting\n To print them, ";
35-
system("pause");
36-
for (int i = 0; i < size; i++)
37-
cout << A[i] << endl;*/
48+
3849
delete[] A;
3950
}
4051

4152
// $$$ Functions Definitions $$$
4253

4354
// InsertionSort Iterative Function
44-
void InsertionSort(int arr[], int startIndex, int endIndex) {
55+
void InsertionSort(int arr[], int startIndex, int endIndex)
56+
{
4557

4658
int i, key, j;
47-
for (i = 1; i <= endIndex; i++)
59+
for (i = startIndex + 1; i <= endIndex; i++)
4860
{
4961
key = arr[i];
5062
j = i - 1;
51-
while (j >= 0 && arr[j] > key) {
63+
// To place the element in the sorted array
64+
while (j >= startIndex && arr[j] > key)
65+
{
5266
arr[j + 1] = arr[j];
5367
--j;
5468
}
5569
arr[j + 1] = key;
5670
}
5771
}
72+
5873
// RandomArray Function to fill the array with random numbers
59-
void RandomArray(int array[], int size) {
74+
void RandomArray(int array[], int size)
75+
{
6076

6177
for (int i = 0; i < size; i++)
6278
array[i] = rand();
63-
64-
}
79+
}

MergeSort.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
1+
2+
// Libraries to be added
3+
14
#include <iostream>
25
#include <cstdlib>
36
#include <iomanip>
7+
#include <chrono>
48
#include <math.h>
59
#include <time.h>
610
#include <fstream>
711

812
using namespace std;
13+
14+
// Function Prototypes
15+
916
void MergeSort(int array[], int start, int end);
1017
void Merge(int array[], int p, int q, int r);
1118
void RandomArray(int array[], int size);
19+
1220
int main()
1321
{
1422
int size;
1523
cout << "Enter the Size of Array: ";
1624
cin >> size;
1725
int *A = new int[size];
26+
27+
// To generate random Numbers and store them
28+
1829
srand(time(NULL));
1930
RandomArray(A, size);
2031

21-
/*cout << "\n Numbers before Sorting\n to print them, ";
22-
system("pause");
23-
for (int i = 0; i < size; i++)
24-
cout << A[i] << endl;
25-
*/
26-
clock_t start, end;
27-
cout << "check1\n";
28-
start = clock();
32+
// Code to call MergeSort and measure time duration
33+
34+
auto start = chrono::high_resolution_clock::now();
2935
MergeSort(A, 0, size - 1);
30-
end = clock();
31-
cout << "check2\n";
32-
float time_taken = float(end - start) / float(CLOCKS_PER_SEC);
33-
cout << "\n Time taken by the Merge Sort,\nwith " << size << " size of Array is: ";
34-
cout << fixed << time_taken << setprecision(7) << " seconds" << endl;
36+
auto end = chrono::high_resolution_clock::now();
37+
double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count();
38+
39+
// Code to calculate the time duration
40+
41+
time_taken *= 1e-9;
42+
cout << "\n Time taken by the Merge Sort, with " << size << " size of Array is: ";
43+
cout << fixed << time_taken << setprecision(9) << " seconds" << endl;
44+
45+
// Code to Save sorted array in csv file
3546

3647
ofstream OutData;
3748
OutData.open("SortedMergeSort.csv");
3849
for (int i = 0; i < size; i++)
3950
OutData << A[i] << endl;
4051

41-
/*cout << "\n\n Numbers after Sorting\n To print them, ";
42-
system("pause");
43-
for (int i = 0; i < size; i++)
44-
cout << A[i] << endl;*/
45-
4652
delete[] A;
4753
return 0;
4854
}
@@ -54,11 +60,15 @@ void MergeSort(int arr[], int startIndex, int endIndex)
5460
if (startIndex < endIndex)
5561
{
5662
int q = floor((startIndex + endIndex) / 2);
63+
// To divide the unsorted Array
5764
MergeSort(arr, startIndex, q);
5865
MergeSort(arr, q + 1, endIndex);
66+
// To merge the sorted array
5967
Merge(arr, startIndex, q, endIndex);
6068
}
6169
}
70+
71+
// Merge function to sort and merge arrays
6272
void Merge(int arr[], int p, int q, int r)
6373
{
6474
int n1 = q - p + 1;
@@ -92,6 +102,7 @@ void Merge(int arr[], int p, int q, int r)
92102
delete[] Left;
93103
delete[] Right;
94104
}
105+
95106
// RandomArray Function to fill the array with random numbers
96107
void RandomArray(int array[], int size)
97108
{

0 commit comments

Comments
 (0)