Metode Merge Sort


Algoritma Merge Sort pada 2 buah data sebagai berikut:

Data A

20 45 30 25 60 200 65 80

Data B

15 150 70 85 120 145 135 185

function merge_sort(m, awal, akhir)
if length(m) = 1
return m
else
tengah = awal+akhir / 2
kiri = mergesort(m, awal,tengah)
kanan= mergesort(m, tengah,akhir)
hasil = merge(kiri,kanan)
return result

Program dalam C++

#include <stdlib.h>#include <iostream.h>

#include <conio.h>

void mergeSort(int arr1[], int arr2[], int arr3[], int size1, int size2, int& size3);

void swap(int& x, int& y);

int main()

{

int nums1[] = { 20, 45, 30, 25, 60, 200, 65, 80 };

int nums2[] = { 15, 150, 70, 85, 120, 145, 135, 185 };

int nums3[16];

int size1 = 6;

int size2 = 8;

int size3;

int k;

cout << “DATA A: “;

for (k = 0; k < size1; k++)

cout << nums1[k] << ” “;

cout << endl << endl;

cout << “DATA B: “;

for (k = 0; k < size2; k++)

cout << nums2[k] << ” “;

mergeSort(nums1, nums2, nums3, size1, size2, size3);

cout << endl << endl;

cout << “HASIL MERGE ARRAY A & B: “;

for (k = 0; k < size3; k++)

cout << nums3[k] << ” “;

cout << endl << endl << endl;

system(“PAUSE”);

return 0;

}// end main()

void mergeSort(int arr1[], int arr2[], int arr3[], int size1, int size2, int& size3)

{

int pos1 = 0, pos2 = 0, pos3 = 0;

while (pos1 < size1 && pos2 < size2)

if (arr1[pos1] < arr2[pos2])

arr3[pos3++] = arr1[pos1++];

else

arr3[pos3++] = arr2[pos2++];

if (pos1 < size1)

while (pos1 < size1)

arr3[pos3++] = arr1[pos1++];

else

while (pos2 < size2)

arr3[pos3++] = arr2[pos2++];

size3 = size1 + size2;

}// end mergeSort()

void swap(int& x, int& y)

{

int temp;

temp = x;

x = y;

y = temp;

}// end swap()

Hasil/Outputnya::

  1. Maret 3, 2010 pukul 8:54 pm

    loh… identasinya gak kluar ya???? jadi bingung aku mahaminnya… maklum ana khan ga pernah kuliah…..

    • nayay18
      Maret 3, 2010 pukul 9:29 pm

      identasi yg dimaksud yg mana??? yg format antara margin n teks atw yg mana…. *hehe malah balik nanya*

  2. November 20, 2012 pukul 4:33 am

    Before making a determination where property brokerage to work with, observe how they discuss.
    Question what kind of coaching and experience they have got.
    You also want to look at the methods they normally use and make sure they are ethical when conducting organization.
    Request to find out the broker’s profile. He should be able to give you samples of profitable discussions. Also request the agent to provide a good example of an not successful negotiation and describe what he acquired from the experience.

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: