0% found this document useful (0 votes)
9 views2 pages

Hal 55-57 Merging - Array

The document is a Pascal program that merges two arrays by first sorting them and then combining them into a third array. It includes procedures for printing arrays, inserting elements in sorted order, and merging two sorted arrays. The program prompts the user for the number of elements in each array, generates random integers, and displays the results before and after merging.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Hal 55-57 Merging - Array

The document is a Pascal program that merges two arrays by first sorting them and then combining them into a third array. It includes procedures for printing arrays, inserting elements in sorted order, and merging two sorted arrays. The program prompts the user for the number of elements in each array, generates random integers, and displays the results before and after merging.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

program MERGING_ARRAY;

uses crt;
var
i,j,k,m,n,y,Temp : integer;
A,B,C : array[1..100] of integer;

Procedure Cetak_array(A:array of integer; x:integer);


var i : integer;
begin
write(' Hasilnya : ');
for i := 0 to x-1 do write(A[i],' ');
end;

procedure sisip(var A : array of integer; x: integer);


var i,j,temp : integer;
begin
for i := 1 to x-1 do
begin
temp := A[i]; j := i;
while (temp < A[j-1]) and (j > 0) do
j := j-1;

for k := i downto j do
A[k] := A[k-1];
A[j] := temp;
end;
end;

procedure Gabung(A,B : array of integer; m,n : integer;


var C : array of integer; var x : integer);
var i,j,k,y : integer;
begin
i := 0; j := 0; k := 0;
while (i <= m) and (j<= n) do
begin
if A[i] < B[j] then
begin
C[k] := A[i];
i := i + 1;
end
end
;
begin
c[k] := B[j];
j := j + 1;
end;
k := k + 1;
write(' i=' ,i,' j=',j,' k=',k,' ');
cetak_array(C,k-1);
writeln;
end;
begin
for y := k-1 to m+n do
begin
C[y] := B[j];
j := j + 1;

for y := k-1 to m+n do


C[y] := A[i];
i := i + 1;
end;

write ('Setelah penggabungan : '); cetak_array(C,y);

begin
clrscr;
randomize;
writeln(' M E R G I N G');
write('Banyaknya elemen array pertama : ');
readln(m);
write('Banyaknya elemen array kedua : ');
readln(n);
for i := 1 to m do

A[i] := random(100);
for i := 1 to n do
B[i] := random(100);

writeln( 'Sebelum Penggabungan : ');


sisip(A,m);
write(' Array pertama : ');
cetak_array(A,m); writeln;
sisip(B,n);
write(' Array kedua : ');
cetak_array(B,n); writeln;

writeln('Proses Penggabungan : ');


Gabung(A,B,m,n,C,y);

writeln;
write('Setelah penggabungan : ');
cetak_array(C,y);
readln;
end;
end.

You might also like