bubble pas
uses wincrt;
const max=100;
var data:array [0..max] of integer;
i,j,n:integer;
procedure input;
begin
write('Masukkan banyak data yang anda inginkan = ');readln(n);
for j:=1 to n do
begin
write('Data[',j,'] = ');
readln(data[j]);
end;
end;
procedure tukar(var a,b:integer);
var temp:integer;
begin
temp:=a;
a:=b;
b:=temp;
end;
procedure bubble_sort_ascending;
begin
for i:=1 to n-1do
for j:=1 to n-i do
if data[j]>data[j+1] then
tukar(data[j],data[j+1]);
end;
procedure bubble_sort_descending;
begin
for i:=1 to n-1do
for j:=1 to n-i do
if data[j]<data[j+1] then
tukar(data[j],data[j+1]);
end;
procedure option;
var pilih:byte;
begin
repeat
write('Pilih (Ascending=1)/(Descending=2)/(Exit=3) = ');
readln(pilih);
if pilih=1 then
begin
bubble_sort_ascending;
for i:=1 to n do write(data[i],' ');
writeln;
end
else if pilih=2 then
begin
bubble_sort_descending;
for i:=1 to n do write(data[i],' ');
writeln;
end
else if pilih=3 then
begin
writeln('tekan Close atau Alt+F4');
exit;
end
else writeln('Option salah!!!!');
until pilih = 3;
exit;
end;
begin
writeln('=================================');
writeln('Program Pengurutan (Sorting) dengan Bubble Sort');
writeln('=================================');
input;
option;
end.
Komentar