Pages

Subscribe:

Ads 468x60px

♪ ♥ Get Paid Daily - The Best Sites PTC ♥ ♪
DonkeyMails.com: No Minimum Payout
AyuWage Services - Get Paid to Visits Sites and Complete Surveys

Minggu, 15 Juli 2012

Pencarian Elemen dalam Array (Binary Search)

BINARY SEARCH – NON GUI
Nama program: binarySearch.java
public class binarySearch {
public static void main(String[] args) {
int array[] = new int[5];
array[0] = 25;
array[1] = 30;
array[2] = 35;
array[3] = 40;
array[4] = 45;
int batasAtas = array.length-1;
int batasBawah = 0;
for(int index = 0 ; index<array.length; index++){
System.out.print(array[index] + ” “);
}
System.out.println(“”);
int cari = 30;
boolean belumKetemu = true;
while(belumKetemu) {
int posisiSekarang = (batasAtas + batasBawah)/2;
if (array[posisiSekarang] == cari) {
belumKetemu=false;
System.out.println(“ditemukan ” + cari);
} else if(batasBawah > batasAtas) {
System.out.println(“tidak ditemukan ” + cari);
break;
}
else {
if (array[posisiSekarang] < cari) {
batasBawah = posisiSekarang + 1;
} else {
batasAtas = posisiSekarang – 1;
}
}
}
}
}
Output program:
25 30 35 40 45
ditemukan 30
BINARY SEARCH – GUI
Nama program: BinarySearchGui.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BinarySearchGui {
JPanel panelKiri = new JPanel();
JPanel panelKanan = new JPanel();
JPanel panelSemua = new JPanel();
JTextField[] fieldArray = new JTextField[10];
JLabel[] label = new JLabel[10];
JTextField sisipElemen = new JTextField(5);
JButton buttonSisip = new JButton(“Sisip”);
JTextField hapusElemen = new JTextField(5);
JButton buttonHapus = new JButton(“Hapus”);
JTextField cariElemen = new JTextField(5);
JButton buttonCari = new JButton(“Cari”);
BinarySearchGui() {
BorderLayout bl = new BorderLayout();
panelSemua.setLayout(bl);
GridLayout gl = new GridLayout(10,2);
panelKiri.setLayout(gl);
for(int x=0; x<fieldArray.length; x++) {
panelKiri.add(label[x] = new JLabel(Integer.toString(x)));
panelKiri.add(fieldArray[x] = new JTextField(5));
}
panelSemua.add(“Center”,panelKiri);
GridLayout gl2 = new GridLayout(3,2);
panelKanan.setLayout(gl2);
panelKanan.add(sisipElemen);
panelKanan.add(buttonSisip);
panelKanan.add(hapusElemen);
panelKanan.add(buttonHapus);
panelKanan.add(cariElemen);
panelKanan.add(buttonCari);
panelSemua.add(“North”,panelKanan);
JFrame jendela = new JFrame(“Array GUI”);
jendela.setContentPane(panelSemua);
jendela.setSize(200,400);
jendela.setResizable(false);
//jendela.pack();
jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jendela.setVisible(true);
BinarySearchProcess proses = new BinarySearchProcess(this);
buttonSisip.addActionListener(proses);
buttonHapus.addActionListener(proses);
buttonCari.addActionListener(proses);
}
public static void main(String[] args) {
BinarySearchGui linearSearch = new BinarySearchGui();
}
}
Nama program: BinarySearchProcess.java
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BinarySearchProcess implements ActionListener{
int array[] = new int[10];
int nElemen = 0;
BinarySearchGui induk;
BinarySearchProcess(BinarySearchGui induk) {
this.induk = induk;
}
public void actionPerformed(ActionEvent kejadian) {
if(kejadian.getSource() == induk.buttonSisip) {
int j;
for(j=0; j<nElemen; j++){
if(array[j] > Integer.parseInt(induk.sisipElemen.getText()))
break;
}
for(int k=nElemen; k>j; k–) {
array[k] = array[k-1];
}
array[j] = Integer.parseInt(induk.sisipElemen.getText());
induk.sisipElemen.setText(“”);
nElemen++;
tampil();
}
else if(kejadian.getSource() == induk.buttonHapus){
int j;
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(“”);
}
for(j=0; j<nElemen; j++){
if( Integer.parseInt(induk.hapusElemen.getText()) == array[j] )
break;
}
if(j==nElemen)
JOptionPane.showMessageDialog(null, “tidak ada”);
else
{
for(int k=j; k<nElemen; k++) {
array[k] = array[k+1];
//nElemen–;
}
nElemen–;
induk.fieldArray[nElemen].setBackground(Color.white);
}
induk.hapusElemen.setText(“”);
tampil();
}
if(kejadian.getSource() == induk.buttonCari) {
int batasAtas = nElemen-1;
int batasBawah = 0;
int cari = Integer.parseInt(induk.cariElemen.getText());
boolean belumKetemu = true;
while(belumKetemu) {
int posisiSekarang = (batasAtas + batasBawah)/2;
if (array[posisiSekarang] == cari) {
belumKetemu=false;
JOptionPane.showMessageDialog(induk.panelSemua, cari + ” ditemukan”);
} else if(batasBawah > batasAtas) {
JOptionPane.showMessageDialog(induk.panelSemua, cari + ” tidak ditemukan”);
break;
}
else {
if (array[posisiSekarang] < cari) {
batasBawah = posisiSekarang + 1;
} else {
batasAtas = posisiSekarang – 1;
}
}
}
induk.cariElemen.setText(“”);
}
}
public void tampil(){
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(” “);
}
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(Integer.toString(array[x]));
}
}
}
Output program:
Pencarian elemen array
Pencarian elemen array

0 komentar:

Posting Komentar

Please Give Your Feedback Or Message.
Thank You!!?

Unique 1:1 Traffic Exchange

♪ ♥ Ma liste de blogs ♥ ♪

♪ ♥ Please Votes! Thanks. ♪ ♥
×