-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hybrid-Merge-Sort.cpp
134 lines (108 loc) · 2.7 KB
/
Hybrid-Merge-Sort.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Libraries to be added
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <chrono>
#include <math.h>
#include <time.h>
#include <fstream>
// Function Prototypes
using namespace std;
void HybridMergeSort(int array[], int start, int end);
void Merge(int array[], int p, int q, int r);
void InsertionSort(int array[], int start, int end);
void RandomArray(int array[], int size);
// main function
int main()
{
int size;
cout << "Enter the Size of Array: ";
cin >> size;
int *A = new int[size];
// To generate random Numbers and store them
srand(time(NULL));
RandomArray(A, size);
// Code to call InsertionSort and measure time duration
auto start = chrono::high_resolution_clock::now();
HybridMergeSort(A, 0, size - 1);
auto end = chrono::high_resolution_clock::now();
double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count();
// Code to calculate the time duration
time_taken *= 1e-9;
cout << "\n Time taken by the Hybrid Merge Sort, with " << size << " size of Array is: ";
cout << fixed << time_taken << setprecision(9) << " seconds" << endl;
// Code to Save sorted array in csv file
ofstream OutData;
OutData.open("SortedHybridMergeSort.csv");
for (int i = 0; i < size; i++)
OutData << A[i] << endl;
delete[] A;
return 0;
}
// $$$ Functions Definitions $$$
// MergeSort Iterative Function
void HybridMergeSort(int arr[], int startIndex, int endIndex)
{
if ((endIndex - startIndex) > 44)
{
int q = floor((startIndex + endIndex) / 2);
// To divide the unsorted Array
HybridMergeSort(arr, startIndex, q);
HybridMergeSort(arr, q + 1, endIndex);
// To merge the sorted array
Merge(arr, startIndex, q, endIndex);
}
InsertionSort(arr, startIndex, endIndex);
}
// Merge function to sort and merge arrays
void Merge(int arr[], int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int *Left = new int[n1 + 1];
int *Right = new int[n2 + 1];
for (int i = 0; i < n1; i++)
Left[i] = arr[p + i];
for (int i = 0; i < n2; i++)
Right[i] = arr[q + 1 + i];
Left[n1] = 147483647;
Right[n2] = 147483647;
int i = 0, j = 0, k = p;
for (k; k <= r; ++k)
{
if (Left[i] <= Right[j])
{
arr[k] = Left[i];
++i;
}
else
{
arr[k] = Right[j];
++j;
}
}
delete[] Left;
delete[] Right;
}
// InsertionSort Iterative Function
void InsertionSort(int arr[], int startIndex, int endIndex)
{
int i, key, j;
for (i = startIndex + 1; i <= endIndex; i++)
{
key = arr[i];
j = i - 1;
while (j >= startIndex && arr[j] > key)
{
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
// RandomArray Function to fill the array with random numbers
void RandomArray(int array[], int size)
{
for (int i = 0; i < size; i++)
array[i] = rand();
}