-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge_sort.cpp
More file actions
81 lines (69 loc) · 1.37 KB
/
merge_sort.cpp
File metadata and controls
81 lines (69 loc) · 1.37 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
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include "solution.h"
using namespace std;
void merge(int input[],int left,int mid,int right)
{
int i=left;
int j=mid;
int k=0;
int temp[right-left+1];
while(i<mid && j<=right)
{
// std::cout<<input[i]<<" "<<input[j];
if(input[i]>input[j])
{
temp[k++]=input[j++];
}
else
{
temp[k++]=input[i++];
}
}
while(i<mid)
{
// std::cout<<input[i];
temp[k++]=input[i++];
}
while(j<=right)
{
// std::cout<<"sdfsdfdsf";
temp[k++]=input[j++];
}
//std::cout<<temp[0]<<temp[1]<<" ";
int s=0;
for(int i=left;i<=right;i++)
{
input[i]=temp[s];
s++;
}
}
void sort(int input[],int left,int right)
{
if(left>=right)
{
return ;
}
else
{
int mid=left +(right-left)/2;
sort(input,left,mid);
sort(input,mid+1,right);
merge(input,left,mid+1,right);
}
}
void mergeSort(int input[], int size){
// Write your code here
int left=0;
int right=size-1;
sort(input,left,right);
}
int main() {
int input[1000],length;
cin >> length;
for(int i=0; i < length; i++)
cin >> input[i];
mergeSort(input, length);
for(int i = 0; i < length; i++) {
cout << input[i] << " ";
}
}