-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
83 lines (65 loc) · 1.5 KB
/
Copy pathMergeSort.cpp
File metadata and controls
83 lines (65 loc) · 1.5 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
82
//
// main.cpp
// Lab02
//
// Created by Andres Rios on 2/1/19.
// Copyright © 2019 Andres Rios. All rights reserved.
//
#include <iostream>
using namespace std;
/* testing if mid is correct
void print(int arr[],int p,int q){
cout<<q<<";"<<endl;
}
*/
void merge(int arr[], int low, int mid, int high){
//a=n1
int a = mid-low+1,i,j,k;
int b = high-mid;
int L[a];
int R[b];
for (int i=0; i<a; i++ ){
//i = low;
L[i]=arr[low+i]; //moves first half of values to l[]
}
for (int j =0; j<b; j++ ){
R[j]=arr[mid+j+1]; //moves next half value into r[]
}
i=0;j=0;
for (k = low;i<a&&j<b; k++ ){
if(L[i]<R[j]){
arr[k] = L[i++];
}
else{
arr[k]=R[j++];
}
}
while(i<a){// merges left array back
arr[k++]=L[i++];
}
while (j<b){// merges right back
arr[k++]=R[j++];
}
}
void merge_sort(int arr[],int low,int high){
int mid;
if(low < high){
mid = (low + high)/2;//looks for middle index
merge_sort(arr,low,mid);//breaks up array to the left(left sub array)
merge_sort(arr,mid+1,high);// breaks array to the right(right sub array)
merge(arr,low,mid,high);
}
}
int main() {
int length;
cin >> length;
int arr[length];
for (int i=0; i<length; i++){
cin>>arr[i];
}
merge_sort(arr,0,length-1);
for(int i=0;i<length;i++)
{
cout << arr[i] << ";";
}
}