-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort.cpp
More file actions
64 lines (54 loc) · 1.75 KB
/
mergesort.cpp
File metadata and controls
64 lines (54 loc) · 1.75 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
// Applying mergesort method
#include<iostream>
using namespace std;
#define watch(x) cout<<(#x)<<" is "<<x<<"\n"
#define print(x) cout<<x<<"\n"
void merge(int arr[], int start, int mid, int end)
{
//stores the starting position of both parts in temporary variables.
int p = start;
int q = mid + 1;
int k = 0; // temparr index
int temparr[end - start + 1]; // take example (n-1 -0 + 1) = n elements
for (int i = start; i <= end; i++){
if (p > mid) //checks if first part comes to an end or not .
temparr[k++] = arr[q++];
else if (q > end) //checks if second part comes to an end or not
temparr[k++] = arr[p++];
else if (arr[p] < arr[q]) //checks which part has smaller element.
temparr[k++] = arr[p++];
else // second array has smaller element. so adding this to temparr.
temparr[k++] = arr[q++];
}
for (int p = 0; p < k; p++)
{
/* Now the real array has elements in sorted manner including both
parts.*/
arr[start++] = temparr[p];
}
}
void merge_sort(int arr[], int start, int end)
{
if (start < end)
{
int mid = (start + end) / 2; // defines the current array in 2 parts .
merge_sort(arr, start, mid); // sort the 1st part of array .
merge_sort(arr, mid + 1, end); // sort the 2nd part of array.
// merge the both parts by comparing elements of both the parts.
merge(arr, start, mid, end);
}
}
void print_arr(int arr[],int n){
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main(){
int arr[]={-1,1,0,-8,99,2,9,9,7,1,2,3};
int n = sizeof(arr)/sizeof(int);
print_arr(arr,n);
merge_sort(arr,0,n-1);
print_arr(arr,n);
return 0;
}