Thursday, July 29, 2010

buble sort pascal

program buble_sort;
uses crt;

var
x:array [0..20] of integer;
i,j,k:integer;

procedure tukar(var a,b:integer);
var temp:integer;
begin
temp:=a;
a:=b;
b:=temp;
end;

procedure input(var n:integer);
var a:integer;
begin
write('masukkan bilangan : ');
readln(a);
n:=a;
for i:=1 to a do
begin
write('masukkan bilangan ke-',i,' : ');
readln(x[i]);
end;
end;

procedure bublesort(var n:array of integer;var p:integer);


begin
for i:=1 to p do begin
for j:=1 to p-i do begin
if x[j] > x[j+1] then
 tukar(x[j],x[j+1]);
 end;end;
end;

procedure cetak(var n:array of integer;var p:integer);
begin
for i:=1 to p do
writeln('nilai ke-',i,' adalah ',n[i]);
writeln;
end;

begin
input(k);
cetak(x,k);
bublesort(x,k);
cetak(x,k);
readln;
end.

No comments: