Selasa, 20 Januari 2009

Source code bubble sort (dalam pascal)

Berikut saya sajikan penerapan salah satu algoritma sorting yang bernama bubble sort

===============================================================================

uses crt;

var
index : byte;
last_compare_at : byte;
n,i,temp : byte;
a : array [1..6] of byte;


begin
n:=6;

clrscr;

for i:=1 to n do
begin
write('Masukkan angka ke - ',i);
write(' : ');
readln(a[i]);
end;

writeln('ini hasil sorting menggunakan bubble sort');

for last_compare_at:=n-1 downto 1 do
begin
for index:=1 to last_compare_at do
begin
if(a[index] > a[index + 1]) then
begin
temp := a[index];
a[index] := a[index + 1];
a[index + 1] := temp;

end;
end;
end;

for i:=1 to n do
begin
write(a[i]);
write(' ');
end;

readln;
end.

Tidak ada komentar: