#include <cstdlib>
#include <iostream>
using namespace std;
void Merge(int* A, int kiri,int tengah, int kanan){
int B[kiri+kanan];
int i,kidal1,kidal2;
kidal1=kiri;
kidal2=tengah+1;
i=kiri;
while (kidal1<=tengah && kidal2 <= kanan){
if(A[kidal1] <= A[kidal2]){
B[i]=A[kidal1];
kidal1++;
}
else{
B[i]=A[kidal2];
kidal2++;
}
i++;
}
while ( kidal1 <= tengah ){
B[i] = A[kidal1];
kidal1++;
i++;
}
while ( kidal2 <= kanan ){
B[i] = A[kidal2];
kidal2++;
i++;
}
for (int i=kiri;i<= kanan;i++){
A[i]=B[i];
}
}
void MergeSort (int* A, int i, int j){
if (i<j){
int k= ((i+j)/2);
MergeSort(A, i, k);
MergeSort(A, k+1, j);
Merge(A, i, k, j);
}
}
int main(int argc, char *argv[])
{
int n,i,j;
cout<<"masukkan Banyak data : ";
cin>>n;
i=1;
j=n;
int A[n];
for (int i=1;i<=n;i++){
cout<<"masukan data ke-"<<i<<" : ";
cin>>A[i];
}
cout<<"data sebelum diurutkan : ";
for (int i=1;i<=j;i++){
cout<<A[i]<<" ";
}
MergeSort(A,i,j);
cout<<endl;
cout<<"data setelah diurutkan : ";
for (int x=1;i<=j;i++){
cout<<A[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
Senin, 19 Mei 2014
PROGRAM MERGESHORT C++
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar