Selasa, 20 Januari 2009

Source code bubble sort (dalam pascal)

Berikut saya sajikan penerapan salah satu algoritma sorting yang bernama bubble sort

===============================================================================

uses crt;

var
index : byte;
last_compare_at : byte;
n,i,temp : byte;
a : array [1..6] of byte;


begin
n:=6;

clrscr;

for i:=1 to n do
begin
write('Masukkan angka ke - ',i);
write(' : ');
readln(a[i]);
end;

writeln('ini hasil sorting menggunakan bubble sort');

for last_compare_at:=n-1 downto 1 do
begin
for index:=1 to last_compare_at do
begin
if(a[index] > a[index + 1]) then
begin
temp := a[index];
a[index] := a[index + 1];
a[index + 1] := temp;

end;
end;
end;

for i:=1 to n do
begin
write(a[i]);
write(' ');
end;

readln;
end.

Source code binary search (dalam pascal)

Berikut saya sajikan tentang penerapan algoritma binary Search. Di mana binary search bekerja saat data sudah diurutkan terlebih dahulu.

===================================================================================

uses crt;

var
a : array[1..10] of byte;
low, high, mid, n, i, x : byte;
found : boolean;

begin
// n menunjukkan banyaknya data yang hendak dimasukkan ke dalam array
n := 10;
low:=1;
high:=n;
found := false;

clrscr;

for i:=1 to 10 do
begin
write('Angka ke- ',i);
write(' : ');
readln(a[i]);
end;

write('Masukkan target yang dicari : ');
readln(x);

while ((low <= high) and (found = false)) do
begin
mid := low + ((high-low) div 2);
if (a[mid] = x) then found:=true
else
if (a[mid] < x) then
low := mid + 1
else
if(a[mid] > x) then
high := mid;
end;

writeln('Data diketemukan di index ke : ',mid);
readln;
end.

Kamis, 15 Januari 2009

Source code Java enkripsi file sederhana

Berikut enkripsi file sederhana misal anda punya file yang berisi karakter abc disimpan dalam file coba1.txt kemudian anda ingin mengenkripsikan dengan mengganti karakter abc menjadi bcd dan disimpan dengan nama file lain...

Berikut listing code-nya :
==================================================================================
import java.io.*;

public class EnkripsiFile
{
public EnkripsiFile(String sumber, String tujuan, int angka)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(sumber);
} catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}

try
{
char data;
int temp, tampung;
FileOutputStream fos;
fos = new FileOutputStream(tujuan);
do
{
//ingat fis.reaad() mengambil kode unicode per karakter
temp = fis.read();
tampung = temp+angka;
//ubah kode ascii jadi huruf
data = (char)tampung;
try
{
fos.write(data);
} catch(FileNotFoundException ex)
{
System.out.println("File cannot opened for writing");
}

} while(temp != -1);
} catch(IOException ex)
{
System.out.println("Problem");
}
}

public static void main(String args[]) throws IOException
{
System.out.println("How many number do you wanto skip ?");
String Strangka;
BufferedReader bl = new BufferedReader(new InputStreamReader(System.in));
Strangka = bl.readLine();

//cara konvert dari kelas String ke tipe integer
int angka = Integer.parseInt(Strangka);

System.out.println("What is the name of the source file ?");
String sumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sumber = br.readLine();
System.out.println("Enter data to write to "+sumber+"...");
System.out.println("What is the name of the aim file ?");
String tujuan;
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
tujuan = bd.readLine();
System.out.println("Enter data to write to "+tujuan+"...");

new EnkripsiFile(sumber,tujuan,angka);

}
}