Thursday, January 20, 2011

Urut data array dengan metode insertion Sort

CALL InputVectorDariRandom(X,N)
document.write" Metode pengurutan data dengan Insertion Sort
"
document.write "
"
CALL Cetak(x,n," Sebelum Sorting :")
CALL insertionSort(x,n)
CALL Cetak(x,n," Setelah Sorting :")

SUB InputVectorDariRandom(byRef v(), byRef N)
RANDOMIZE
N = cint(InputBox("Masukkan Jumlah Elemen Array"))
REDIM v(N)
FOR c=1 TO N
v(c)=10+int(rnd*90)
NEXT
END SUB

SUB insertionSort(byRef v(), byVal N)
FOR i=1 TO N
temp=v(i)
j=i-1
do while (j>=0) and (v(j)>temp)
v(j+1)=v(j)
j=j-1
loop
v(j+1)=temp
NEXT
END SUB

SUB Cetak(byVal v(), byVal N, message)
document.write "Isi Vektor " & message & "
"
FOR c=1 TO N
document.write v(c) & ", "
NEXT
document.write "
"
document.write "
"
END SUB

No comments:

Post a Comment