forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
three_way_merge_sort.cpp
172 lines (140 loc) · 3.57 KB
/
three_way_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
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
// C++ program to implement Three Way Merge Sort
#include <bits/stdc++.h>
using namespace std;
// Merge the three sections in ascending order
void merge(int arr[], int beg, int midA, int midB, int end, int temp[])
{
int i, j, k, l;
i = beg;
j = midA;
k = midB;
l = beg;
// Find the smallest element among the three sections
while ((i < midA) && (j < midB) && (k < end))
{
if(arr[i] < arr[j])
{
if(arr[i] < arr[k])
{
temp[l++] = arr[i++];
}
else
{
temp[l++] = arr[k++];
}
}
else
{
if(arr[j] < arr[k])
{
temp[l++] = arr[j++];
}
else
{
temp[l++] = arr[k++];
}
}
}
/*
Now any two arrays would have remaining values that are yet to be merged,
We have to identify those two arrays and merge the elements.
*/
while ((i < midA) && (j < midB))
{
if(arr[i] < arr[j])
{
temp[l++] = arr[i++];
}
else
{
temp[l++] = arr[j++];
}
}
while ((j < midB) && (k < end))
{
if(arr[j] < arr[k])
{
temp[l++] = arr[j++];
}
else
{
temp[l++] = arr[k++];
}
}
while ((i < midA) && (k < end))
{
if(arr[i] < arr[k])
{
temp[l++] = arr[i++];
}
else
{
temp[l++] = arr[k++];
}
}
/*
Now a single array would have remaining values that are yet to be merged,
We have identify that array and copy its remaining elements.
*/
while (i < midA)
temp[l++] = arr[i++];
while (j < midB)
temp[l++] = arr[j++];
while (k < end)
temp[l++] = arr[k++];
}
void three_way_merge_sort(int arr[], int beg, int end, int temp[])
{
//If the array contains only a single element, then return
if (end - beg < 2)
return;
// Split the array into three parts
int midA = beg + ((end - beg) / 3);
int midB = beg + 2 * ((end - beg) / 3) + 1;
three_way_merge_sort(temp, beg, midA, arr);
three_way_merge_sort(temp, midA, midB, arr);
three_way_merge_sort(temp, midB, end, arr);
// Merge the sorted arrays
merge(temp, beg, midA, midB, end, arr);
}
int main()
{
int n;
cout<<"\nHow many numbers do you want to sort? ";
cin>>n;
int arr[n];
if (n <= 0)
{
cout<<"There are no numbers to sort!!!";
return 0;
}
// Input the numbers to sort
cout<<"Enter the numbers: ";
for (int i = 0; i < n; i++)
cin>>arr[i];
// creating a temporary array
int temp[n];
// Copy the elements of the temporary elements into the new array
for (int i = 0; i < n; i++)
temp[i] = arr[i];
//Call the sort function
three_way_merge_sort(temp, 0, n, arr);
cout<<"The numbers in sorted order is: ";
// Print the sorted array
for (int i = 0; i < n; i++)
cout<<temp[i]<<" ";
cout<<endl;
return 0;
}
/*
Time Complexity: O(n*logn)
Space Complexity: O(n)
SAMPLE INPUT AND OUTPUT
SAMPLE 1
How many numbers do you want to sort? 5
Enter the numbers: 1 3 5 2 4
The numbers in sorted order is: 1 2 3 4 5
SAMPLE 2
How many numbers do you want to sort? 0
There are no numbers to sort!!!
*/