Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int nilai[5];
- void combine(int a[ ],int p,int q,int r)
- {
- int c[5];
- int i = p;
- int j = q+1;
- int k = p;
- while((i<=q) && (j<=r))
- {
- if (a[i]<a[j])
- {
- c[k] = a[i];
- i=i+1;
- k=k+1;
- }
- else
- {
- c[k] = a[j];
- j=j+1;
- k=k+1;
- }
- }
- while(i<=q)
- {
- c[k] = a[i];
- i=i+1;
- k=k+1;
- }
- while(j<=r)
- {
- c[k] = a[j];
- j=j+1;
- k=k+1;
- }
- int l=p;
- while(l<=r)
- {
- a[l] = c[l];
- l=l+1;
- }
- }
- void mergesort(int a[ ],int low,int high)
- {
- int mid;
- if (low<high)
- {
- mid=(low+high)/2;
- mergesort(a,low,mid);
- mergesort(a,mid+1,high);
- combine(a,low,mid,high);
- }
- }
- int main()
- {
- cout<<"\t\t--||CONTOH PROGRAM MERGE SORT||--"<<endl;
- cout<<"enter the elements"<<endl;
- for(int i=0 ; i<5 ;i++)
- {
- cin>>nilai[i];
- }
- mergesort(nilai,0,4);
- cout<<"sorted array"<<endl;
- for(int i=0 ; i<5 ; i++)
- {
- cout<<nilai[i]<<", ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement