Monday, 25 May 2015

Marge Sort

#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){

    int k;

  

    if (i<j){

             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;

    int i;

    int j;

    cout<<"Masukkan junlah data : ";

    cin>>n;

    i=1;

    j=n;

    int A[n];

    for (int x=1;x<=n;x++){

                          cout<<"masukan data ke-"<<x<<" : ";

                          cin>>A[x];

                          }

    cout<<"Data sebelum diurutkan : ";

    for (int x=1;x<=j;x++){

                          cout<<A[x]<<" ";

                          }

    cout<<endl;

    MergeSort(A,i,j);

    cout<<"Data sesudah diurutkan : ";

    for (int x=1;x<=j;x++){

                          cout<<A[x]<<" ";

                          }

    cout<<endl;

  

system("pause");

return 0;

}

No comments:

Post a Comment

Konversi Suhu

#include <iostream> #include <conio.h> //#include <cstdlib> //#include <iostream.h> void main( float Celcius, Kelvi...