forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_sort.cpp
75 lines (67 loc) · 1.98 KB
/
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
#include <bits/stdc++.h>
using namespace std ;
class Solution {
public:
void merge(int arr[], int l, int mid, int r)
{
int i = l ; // starting index of left half of arr
int j = mid + 1; // starting index of right half of arr
int f = l ; // index used to transfer elements in temporary array
int temp[100000] ; // temporary array
//storing elements in the temporary array in a sorted manner//
while (i <= mid && j <= r) {
if (arr[i] < arr[j]) {
temp[f] = arr[i] ;
i++ ;
}
else {
temp[f] = arr[j] ;
j++ ;
}
f++ ;
}
// if elements on the left half are still left //
if (i > mid) {
while (j <= r) {
temp[f] = arr[j] ;
f++ ; j++ ;
}
}
else {
// if elements on the right half are still left //
while (i <= mid) {
temp[f] = arr[i] ;
f++ ; i++ ;
}
}
// transfering all elements from temporary to arr //
for (int f = l ; f <= r; f++) {
arr[f] = temp[f] ;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int mid = (l + r) / 2 ;
mergeSort(arr, l, mid) ; // left half
mergeSort(arr, mid + 1, r) ; // right half
merge(arr, l, mid, r) ; // merging sorted halves
}
}
};
int main() {
int arr[] = {9, 4, 7, 6, 3, 1, 5} ;
int n = 7;
Solution obj ;
cout << "Before Sorting Array: "<<endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout<<endl;
obj.mergeSort(arr, 0, n - 1);
cout << "After Sorting Array: "<<endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
return 0 ;
}