program merge_sort;
uses crt;
Type
larik=array[1..100] of integer;
var
bil:larik;
i, j, n:integer;
procedure Merge(var A1,A2:larik;awal,mid,akhir:integer);
var
o,p,s,i:integer;
begin
o:=awal;
p:=awal;
s:=mid+1;
for i := awal to akhir do
A2[i]:=A1[i];
while (o<=mid) and (s<=akhir) do
begin
if A2[o]<A2[s] then
begin
A1[p]:=A2[o];
inc(o);
end
else
begin
A1[p]:=A2[s];
inc(s);
end;
inc(p);
end;
if s>awal then
for i := o to mid do
begin
A1[p]:=A2[i];
inc(p);
end
else
for i := s to akhir do
begin
A1[p]:=A2[i];
inc(p);
end;
end;
procedure MergeSort(var A1:larik; awal,akhir:integer);
var
mid:integer;
A2:larik;
begin
if awal<akhir then
begin
mid:=(awal+akhir) div 2;
MergeSort(A1,awal,mid);
MergeSort(A1,mid+1,akhir);
Merge(A1,A2,awal,mid,akhir);
end;
end;
Begin
Clrscr;
write('input banyak data : ');
readln(n);
for i := 1 to n do
begin
write('Masukkan Bilangan Ke-',i,' : ');
readln(bil[i]);
end;
MergeSort(bil,1,n);
writeln('Hasil Pengurutan Data : ');
for i := 1 to n do
begin
writeln('bilangan ke-',i, ' : ', bil[i]);
end;
readln;
End.
uses crt;
Type
larik=array[1..100] of integer;
var
bil:larik;
i, j, n:integer;
procedure Merge(var A1,A2:larik;awal,mid,akhir:integer);
var
o,p,s,i:integer;
begin
o:=awal;
p:=awal;
s:=mid+1;
for i := awal to akhir do
A2[i]:=A1[i];
while (o<=mid) and (s<=akhir) do
begin
if A2[o]<A2[s] then
begin
A1[p]:=A2[o];
inc(o);
end
else
begin
A1[p]:=A2[s];
inc(s);
end;
inc(p);
end;
if s>awal then
for i := o to mid do
begin
A1[p]:=A2[i];
inc(p);
end
else
for i := s to akhir do
begin
A1[p]:=A2[i];
inc(p);
end;
end;
procedure MergeSort(var A1:larik; awal,akhir:integer);
var
mid:integer;
A2:larik;
begin
if awal<akhir then
begin
mid:=(awal+akhir) div 2;
MergeSort(A1,awal,mid);
MergeSort(A1,mid+1,akhir);
Merge(A1,A2,awal,mid,akhir);
end;
end;
Begin
Clrscr;
write('input banyak data : ');
readln(n);
for i := 1 to n do
begin
write('Masukkan Bilangan Ke-',i,' : ');
readln(bil[i]);
end;
MergeSort(bil,1,n);
writeln('Hasil Pengurutan Data : ');
for i := 1 to n do
begin
writeln('bilangan ke-',i, ' : ', bil[i]);
end;
readln;
End.
0 comments:
Posting Komentar