Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge_sort.cpp #1

Merged
merged 2 commits into from
Oct 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Problem Solving/merge_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<iostream>
using namespace std;
void merge(int arr[],int start1,int start2,int end){
int k = start1;
int m = start2-1;
int n = end;
int temp[100];
int index=0;
while(start1<=m && start2<=n){
if(arr[start1]<arr[start2]){
temp[index++] = arr[start1++];
}else{
temp[index++] = arr[start2++];
}
}
while(start1<=m){
temp[index++] = arr[start1++];
}
while(start2<=n){
temp[index++] = arr[start2++];
}
for(int i=0;i<index;i++){
arr[k+i] = temp[i];
}
return;
}
void mergeSort(int arr[],int start,int end){
if(start>=end){
return;
}
int mid = (start+end)/2;
mergeSort(arr,start,mid);
mergeSort(arr,mid+1,end);
merge(arr,start,mid+1,end);
return;
}
int main(){
int arr[100] = {5,1,7,2,9};
mergeSort(arr,0,4);
for(int i=0;i<5;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}