pascal: buble sort menaik & menurun

program arh_bsort_menaik;

uses wincrt;

var i,n,j : integer;
a: array [1..100] of integer;

procedure buble;
var z: integer;
begin
for i:= 1 to n-1 do
begin
for j:= n downto i+1 do
begin
if a[j] < a[j-1] then
begin
z:= a[j];
a[j]:= a[j-1];
a[j-1]:=z;
end;
end;
end;

end;
begin
write(‘masukkan banyak larik (maks 100) : ‘); readln(n);
for i:= 1 to n do
begin write(‘A[‘,i,’] : ‘); readln(a[i]);
end;

buble;
write(‘data setelah diurutkan : ‘);

for j:=1 to n do
write (a[j],’ ‘);
end.

 

 

 

 

Program Bubble_Sort;

Uses WinCrt;

const

max = 100;

type

Larik = array [1..max] of integer;

var

A: Larik;

I: integer;

N: integer;

pil:byte;

bilangan,frek,x:integer;

procedure Jumlah_Data;

begin

write(‘Masukkan banyaknya data = ‘); readln(N);

writeln;

end;

procedure Input;

var

I: integer;

begin

for I:=1 to N do

begin

write(‘Masukkan data ke-’, I, = ‘); readln(A[I]);

end;

end;

procedure Change(var A, B: integer);

var

T: integer;

begin

T:=A;

A:=B;

B:=T;

end;

procedure asc_buble;

var

p,q :INTEGER;

flag:boolean;

begin

flag:=false;

p:=2;

while (p<N) and (not flag) do

begin

flag:=true;

for q:=N downto p do

if A[q]<A[q-1] then

begin

change(A[q],A[q-1]);

flag:=false;

end;

inc(i);

end;

writeln;

write(‘Data Diurutkan Secara Ascending: ‘);

end;

procedure desc_buble;

var

p,q :byte;

flag:boolean;

begin

flag:=false;

p:=2;

while (p<max) and (not flag) do

begin

flag:=true;

for q:=max downto p do

if A[q]>A[q-1] then

begin

change(A[q],A[q-1]);

flag:=false;

end;

inc(i);

end;

writeln;

write(‘Data Diurutkan Secara Descending: ‘);

end;

procedure Output;

var

i: integer;

begin

for i:=1 to N do

write(A[i], ‘  ‘);

writeln;

end;

begin

Jumlah_Data;

input;

clrscr;

writeln(‘[1].pengurutan secara Ascending’);

writeln(‘[2].pengurutan secara Descending’);

write(‘Silahkan Masukkan Pilihan Anda = ‘);readln(pil);

case pil of

1:asc_buble;

2:desc_buble;

end;
output;

begin
procedure seq_
write(‘angka yang dicari=’); readln(x);
i:=1;
while (i<5) and (A[i] <> x) do
begin
i:=i+1;
end;
if (A[i]=x) then
writeln(‘ditemukan pada elemen larik ke-‘,i)
else
writeln(‘tidak ditemukan!!’);
end;
begin
seq_search;
end.

end.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s