Pengurutan data dengan BubbleSort di PASCAL


program bubblesorting;
uses
wincrt;
type
array100 = array[1..100] of integer;
var
data :array100;
datacount:integer;

procedure input(var d:array100;var c :integer);
var
code,k :integer;
i:string;
begin
k:=1;
repeat
write('masukkan angka ke- ',k,':');readln(i);
if(i<>'') then
begin
val(i,d[k],code);
if(code<>0)then
d[k]:=0;
end;
inc(k);
until(k>100)or(i='');
c:=k-2;
end;
procedure swap(var a,b : integer);
var
t :integer;
begin
t:=a;
a:=b;
b:=t;
end;

procedure bubblesort(var d:array100;c:integer);
var
i,j : integer;
begin
for i:=1 to c -1 do
for j:=c downto i+1 do
if(d[j] swap(d[j],d[j-1]);
end;

procedure tampil(d:array100;c:integer);
var
i:integer;
begin
for i:=1 to c do
write(d[i]:5);
writeln;
end;

begin
input(data,datacount);
writeln('sebelum diurutkan');
tampil(data,datacount);
bubblesort(data,datacount);
writeln;
writeln('setelah data diurutkan');
tampil(data,datacount);
end.

Tidak ada komentar:

Diberdayakan oleh Blogger.