0% found this document useful (0 votes)
6 views4 pages

Sorting Algorithms in Pascal and Java

The document contains a Pascal program that implements various sorting algorithms including bubble sort, selection sort, insertion sort, gravity sort, and merge sort. It also includes functionality for reading from and writing to files, as well as handling user input through a Java program. The code is structured to initialize output files, copy arrays, and manage sequences of integers for sorting.

Uploaded by

bnl9pstqk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Sorting Algorithms in Pascal and Java

The document contains a Pascal program that implements various sorting algorithms including bubble sort, selection sort, insertion sort, gravity sort, and merge sort. It also includes functionality for reading from and writing to files, as well as handling user input through a Java program. The code is structured to initialize output files, copy arrays, and manage sequences of integers for sorting.

Uploaded by

bnl9pstqk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM sorting; end;

uses crt;
TYPE IntArray = array of Integer; procedure appendSequenceToFile(arr: IntArray);
var
function getSequenceFromFile:IntArray; f: TextFile;
var i: integer;
f:TextFile; begin
linestr:String; assign(f,'[Link]');
outArr:IntArray; append(f);
n,c:integer; for i:=1 to Length(arr) do write(f, arr[i], ' ');
begin writeln(f,'');
assign(f,'[Link]'); close(f);
reset(f); end;

SetLength(outArr, 0); function copyArray(srcArr: IntArray):IntArray;


var
while not eof(f) do outArr: IntArray;
begin i:integer;
ReadLn(f,linestr); begin
begin SetLength(outArr,Length(srcArr));
SetLength(outArr, Length(outArr)+1); for i:=1 to Length(srcArr) do outArr[i]:=srcArr[i];
Val(linestr,n,c); Exit(outArr);
if c<>0 then write('error on c=',c); end;
outArr[High(outArr)] := n;
end; procedure printArray(srcArr:IntArray; minBorder,
end; maxBorder:integer);
close(f); var
Exit(outArr); tempik: integer;
end; begin
for tempik:=minBorder to maxBorder do
procedure initializeOutputFile; write(srcArr[tempik],' ');
var f:TextFile; end;
begin
assign(f,'[Link]'); function
rewrite(f); sortBubble(srcArr:IntArray;mode:integer):IntArray;
close(f); var
i,j,t:integer; end
arr:IntArray; end;
begin writeln;
arr := copyArray(srcArr); end;
for i:=Length(arr)-1 downto 1 do Exit(arr);
begin end;
for j:=1 to i do
begin function
if arr[j]>arr[j+1] then sortSelection(srcArr:IntArray;mode:Integer):IntArray;
begin var
t:=arr[j]; idx,i,j,m: Integer;
arr[j]:=arr[j+1]; arr: IntArray;
arr[j+1]:=t; begin
end arr := copyArray(srcArr);
end; for i:= 1 to Length(arr)-1 do
writeln; begin
end; m:= i;
Exit(arr); for j:= i + 1 to Length(arr) do
end; if arr[j] < arr[m] then m:= j;
idx:= arr[m];
function arr[m]:= arr[i];
sortGravity(srcArr:IntArray;mode:integer):IntArray; arr[i]:= idx;
var end;
i,j,t:integer; Exit(arr);
arr:IntArray; end;
begin
arr := copyArray(srcArr); function
for i:=1 to Length(arr)-1 do sortInsertion(srcArr:IntArray;mode:Integer):IntArray;
begin var
for j:=Length(arr)-1 downto i do i,j,idx: integer;
begin arr: IntArray;
if arr[j]>arr[j+1] then begin
begin arr := copyArray(srcArr);
t:=arr[j]; for i:=2 to Length(arr) do
arr[j]:=arr[j+1]; begin
arr[j+1]:=t; idx := arr[i];
j := i; {conquer}
while ((j>1) AND (arr[j-1]>idx)) do i:=l; j:=m+1; c:=l;
begin write('merging ');printArray(arr,l,m);write('
arr[j] := arr[j-1]; and ');printArray(arr,m+1,r);writeln;
j := j-1; while (c<=r) do
end; begin
arr[j] := idx; if arr[i]<arr[j] then begin
end; tempArr[c] := arr[i];
Exit(arr); inc(i); inc(c);
end; end else begin
tempArr[c] := arr[j];
function sortMerge(srcArr:IntArray;mode:Integer):IntArray; inc(j); inc(c);
var end;
arr,tempArr: IntArray; printArray(tempArr,l,c-1); writeln;
i,j,m,t,c: integer; end;
{i,j left right index, m middle, t temp, c conquer for k:=l to r do
index} begin
procedure mergeSwap(a,b:Integer); arr[k]:=tempArr[k];
begin end;
t:=arr[a]; write('sorted : '); printArray(arr,l,r);
arr[a]:=arr[b]; writeln;
arr[b]:=t; end;
end; end;
procedure doSort(l,r:Integer); begin
var arr := copyArray(srcArr);
k:integer; tempArr := copyArray(srcArr);
begin doSort(1,Length(arr));
writeln('l=',l,' | r=',r); Exit(arr);
write('arr : '); printArray(arr,l,r); writeln; end;
if (r-l=1) and (arr[l]>arr[r]) then mergeSwap(l,r)
else if r-l>=2 then VAR
begin numArr: IntArray;
m := (r+l) div 2; BEGIN
{divide} initializeOutputFile;
doSort(l,m); numArr := getSequenceFromFile;
doSort(m+1,r); appendSequenceToFile(numArr);
appendSequenceToFile(sortBubble(numArr,0)); [Link]("Input Nilai Akhir : ");int b
appendSequenceToFile(sortGravity(numArr,0)); = [Link]([Link]());
appendSequenceToFile(sortSelection(numArr,0));
appendSequenceToFile(sortInsertion(numArr,0)); [Link]("===============================");
appendSequenceToFile(sortMerge(numArr,0)); int t = 0; int x;
END. [Link]("Output: ");
import [Link]; do {
x = a%d;
public class soal1 { if (x==0) {
public static void main(String[] args) { t+=a;
Scanner sc = new Scanner([Link]); [Link]("%d ",a);
[Link]("Input Sebuah angka : "); }
int inp = [Link]([Link]()); a++;
} while (a<=b);
[Link]("================================="); [Link]("\nTotal : %d",t);
int i = 1; [Link]();
int rem, res; }
while (i<5) { }
res = inp/(i*2);
rem = inp%(i*2); public class soal3 {
[Link]("%d : %d = %d sisa %d\ public static void main(String[] args) {
n",inp, i*2, res, rem); Scanner sc = new Scanner([Link]);
i++; [Link]("Jumlah baris: ");
} int m = [Link]([Link]());
[Link](); for (int i=1; i<=m; i++) {
} for (int j=0; j<i; j++) {
} [Link]("%d ",j+i);
}
public class soal2 { [Link]();
public static void main(String[] args) { }
Scanner sc = new Scanner([Link]); [Link]();
[Link]("Input Nilai Pembagi : ");int d = }
[Link]([Link]()); }
[Link]("Input Nilai Awal : ");int a
= [Link]([Link]());

You might also like