Created
November 26, 2013 12:24
-
-
Save shaina7837/7657507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* MERGE SORT */ | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
void mergepass (vector<int> &a, int low, int mid, int high) | |
{ | |
vector<int> b(20); | |
int h = low, i = low, j = mid+1, k; | |
while((h<=mid) && (j<=high)){ | |
if(a[h] < a[j]){ | |
b[i++] = a[h++]; | |
} | |
else{ | |
b[i++] = a[j++]; | |
} | |
} | |
if(h>mid){ | |
for(k = j; k<=high; k++){ | |
b[i] = a[k]; | |
i++; | |
} | |
} | |
else{ | |
for(k=h; k<=mid; k++){ | |
b[i] = a[k]; | |
i++; | |
} | |
} | |
for( k=low; k<=high; k++) | |
a[k] = b[k]; | |
} | |
void mergesort(vector<int> &a, int low, int high) | |
{ | |
if(low != high){ | |
int mid= (low+high)/2; | |
mergesort(a,low, mid); | |
mergesort(a, mid+1, high); | |
mergepass(a, low, mid, high); | |
} | |
} | |
main() | |
{ | |
vector<int> v(20); | |
int n, low, high; | |
cout<<"enter number of elements"; | |
cin>> n; | |
cout<<"enter values"<<endl; | |
for(int i=1; i<=n; i++){ | |
cout<<i<<": "; | |
cin>>v[i]; | |
} | |
low = 1; high = n; | |
mergesort(v, low, high); | |
for (int j=1; j<=n; j++) | |
cout<<v[j]<<" "; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment