forked from sharduls27/cpp-coding-hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.cpp
More file actions
69 lines (62 loc) · 1.45 KB
/
merge_sort.cpp
File metadata and controls
69 lines (62 loc) · 1.45 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
65
66
67
68
69
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class merge_sort
{
public:
void merge(int arr[], int l, int m, int r)
{
int left =l;
int right=m+1;
vector<int>temp;
while(left<=m && right<=r){
if(arr[left]>arr[right])
{
temp.push_back(arr[right]);
right++;
}
else{
temp.push_back(arr[left]);
left++;
}
}
while(left<=m){
temp.push_back(arr[left]);
left++;
}
while(right<=r){
temp.push_back(arr[right]);
right++;
}
for(int i=l; i<=r; i++){
arr[i]=temp[i-l];
}
}
public:
void mergeSort(int arr[], int l, int r)
{
//code here
if(l == r) return;
int m=(l+r)>>1;
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
};
int main()
{
int size;
cout<<"Enter the size of the Array that is to be sorted: "; cin>>size;
int Hello[size],i;
cout<<"Enter the elements of the array one by one:n";
for(i=0; i<size; i++) cin>>Hello[i];
mergeSort(Hello, 0, size - 1);
cout<<"The Sorted List isn";
for(i=0; i<size; i++)
{
cout<<Hello[i]<<" ";
}
return 0;
}