Pengurutan data dengan SelectionSort di PASCAL


program selectionsorting;
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 selectionsort(var d:array100;c:integer);
var
lok,i,j : integer;
begin
for i:=1 to c-1 do
begin
lok:=i;
for j:=i+1 to c do
if(d[j] lok:=j;
swap(d[i],d[lok]);
end;
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);
selectionsort(data,datacount);
writeln;
writeln('setelah data diurutkan');
tampil(data,datacount);
end.

Tidak ada komentar:

Diberdayakan oleh Blogger.