Skip to content

Commit

Permalink
implement merge sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanish2409 committed Aug 7, 2021
1 parent 3cb04ef commit 95b4d7e
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions merge_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include<bits/stdc++.h>
using namespace std;

void merge(vector<int>& arr, int l, int mid, int r){
int l1 = mid - l + 1;
int l2 = r - mid;
vector<int> a1;
vector<int> a2;

for(int i = 0; i < l1; i++) {
a1.emplace_back(arr[l + i]);
}
for(int i = 0; i < l2; i++) {
a2.emplace_back(arr[mid + 1 + i]);
}

int i = 0, j = 0, k = l;

while(i < l1 && j < l2) {
if(a1[i] < a2[j]) {
arr[k++] = a1[i++];
}else {
arr[k++] = a2[j++];
}
}

while(i < l1) {
arr[k++] = a1[i++];
}
while(j < l2) {
arr[k++] = a2[j++];
}
}

void mergeSort(vector<int>& arr, int l, int r) {

if(l < r) {
int mid = (l + r) / 2;
mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, r);

merge(arr, l, mid, r);
}


}

int main() {
int n;
cin>>n;
int count = 0;

vector<int> arr;

for(int i = 0; i < n; i++) {
int x;
cin>>x;

arr.emplace_back(x);
}

mergeSort(arr, 0, n-1);

for(auto it: arr) {
cout<<it<<" ";
}
cout<<endl;
cout<<count<<endl;
}

0 comments on commit 95b4d7e

Please sign in to comment.