7. Mari kita lihat daftar nomor dari sebuah array untuk melihat bagaimana cara merge sort
bekerja :
32 12 5 18 31 4 25 7
[0] [1] [2] [3] [4] [5] [6] [7]
Lakukan sorting dari data dalam array di atas menggunakan merge sort sehingga nomor
paling kecil berada paling depan samapai yang paling besar berada paling belakang
Source Code :
#include <iostream>
#include <conio.h>
using namespace std;
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h]; h++;
}
else
{
b[i]=a[j]; j++;
} i++;
}
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];
}
int main()
{
int num,i,b;
cout<<"***************************"<<endl;
cout<<" MERGE SORT PROGRAM "<<endl;
cout<<"***************************"<<endl;
cout<<endl<<endl;
cout<<"Masukkan Banyak Bilangan: ";cin>>num;
cout<<endl;
cout<<"Sekarang masukkan "<< num <<" Bilangan yang ingin Diurutkan :"<<endl;
for(b=1;b<=num;b++)
{
cout<<"Bilangan ke-"<<b<<" : ";cin>>a[b] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"Hasil akhir pengurutan :"<<endl;
cout<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
getch();
}
Output :
bekerja :
32 12 5 18 31 4 25 7
[0] [1] [2] [3] [4] [5] [6] [7]
Lakukan sorting dari data dalam array di atas menggunakan merge sort sehingga nomor
paling kecil berada paling depan samapai yang paling besar berada paling belakang
Source Code :
#include <iostream>
#include <conio.h>
using namespace std;
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h]; h++;
}
else
{
b[i]=a[j]; j++;
} i++;
}
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];
}
int main()
{
int num,i,b;
cout<<"***************************"<<endl;
cout<<" MERGE SORT PROGRAM "<<endl;
cout<<"***************************"<<endl;
cout<<endl<<endl;
cout<<"Masukkan Banyak Bilangan: ";cin>>num;
cout<<endl;
cout<<"Sekarang masukkan "<< num <<" Bilangan yang ingin Diurutkan :"<<endl;
for(b=1;b<=num;b++)
{
cout<<"Bilangan ke-"<<b<<" : ";cin>>a[b] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"Hasil akhir pengurutan :"<<endl;
cout<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
getch();
}
Output :
Comments
Post a Comment