Tuliskan procedure insertionshort() pada class arrayint
kokosip
Ini pake pascal,, yg C dibawah... ------------------------------------------------------------------------------------------------ program insertion(input,output);
const MAX = 100; var a : array[1..MAX] of integer; i, n : integer;
procedure insertionsort; var i, pos : integer; nilai : integer; tanda : boolean;
begin for i := 2 to n do begin nilai := a[i]; pos := i; tanda := false; while not tanda do begin if pos <= 1 then tanda := true else if nilai >= a[pos-1] then tanda := true else begin a[pos] := a[pos-1]; pos := pos-1 end end; {while}
a[pos] := nilai; end {for} end;
begin { main } write('Masukkan banyak data (max=',MAX:2,') : '); readln(n);
writeln('Masukkan Angka sebanyak ',n:1,' : '); for i := 1 to n do read(a[i]);
insertionsort;
for i := 1 to n do write(a[i]:1,' '); writeln; end.
Ini pake bahasa C -------------------------------------------------------------------------------------------------- #include <stdio.h>
int main() { int n, array[1000], c, d, t; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) { scanf("%d", &array[c]); }
for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } }
printf("Sorted list in ascending order:\n"); for (c = 0; c <= n - 1; c++) { printf("%d\n", array[c]); } return 0; }
------------------------------------------------------------------------------------------------
program insertion(input,output);
const
MAX = 100;
var
a : array[1..MAX] of integer;
i, n : integer;
procedure insertionsort;
var
i, pos : integer;
nilai : integer;
tanda : boolean;
begin
for i := 2 to n do
begin
nilai := a[i];
pos := i;
tanda := false;
while not tanda do
begin
if pos <= 1 then
tanda := true
else if nilai >= a[pos-1] then
tanda := true
else
begin
a[pos] := a[pos-1];
pos := pos-1
end
end; {while}
a[pos] := nilai;
end {for}
end;
begin { main }
write('Masukkan banyak data (max=',MAX:2,') : ');
readln(n);
writeln('Masukkan Angka sebanyak ',n:1,' : ');
for i := 1 to n do
read(a[i]);
insertionsort;
for i := 1 to n do
write(a[i]:1,' ');
writeln;
end.
Ini pake bahasa C
--------------------------------------------------------------------------------------------------
#include <stdio.h>
int main() {
int n, array[1000], c, d, t;
printf("Enter number of elements\n"); scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t; d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}