Source code disini saya menggunakan bahasa pemrograman JAVA
import javax.swing.JOptionPane;
public class BinarySearchRekursif {
private static int search(int key, int[] dt, int idxLeft, int idxRight) {
int idxMid = idxLeft + (idxRight - idxLeft) / 2;
if(idxLeft > idxRight) {
return -1;
} else if(key == dt[idxMid]) {
return idxMid;
} else if(key < dt[idxMid]){
return search(key, dt , idxLeft, idxMid - 1);
} else {
return search(key, dt, idxMid + 1, idxRight);
}
}
public static void main(String args[]) {
int [] data = {2, 5, 8, 10, 14, 32, 35, 41, 67, 88, 90, 101, 109};
String keyStr = JOptionPane.showInputDialog("Data yang dicari:");
int keyInt = Integer.parseInt(keyStr);
int hasil = search(keyInt, data, 0, data.length - 1);
String pesan = hasil >= 0 ? "Data ditemukan pada index: " + hasil :
"Data tidak ditemukan";
JOptionPane.showMessageDialog(null, pesan);
}
}
All About JAvA Programming
Categories
- 2 (1)
- array (1)
- balok (1)
- binary (1)
- bukan prima (1)
- cari (1)
- demopassword (1)
- else (2)
- fibonanci (1)
- float (1)
- for (1)
- hitung (1)
- if (2)
- import (2)
- int (4)
- io (1)
- james gosling (1)
- java (2)
- java1 (1)
- java2 (1)
- javax (2)
- jcreator (1)
- Joptionpane (6)
- konversi (3)
- lebar (1)
- luas (2)
- null (1)
- oak (1)
- OOP (1)
- p.l.t (1)
- panjang (1)
- parseInt (1)
- password (1)
- piramid (1)
- prima (1)
- public (1)
- rekursif (1)
- return (1)
- search (1)
- searching (1)
- segitiga (1)
- showinputdialog (2)
- sisi (1)
- sun microsystems (1)
- swing (1)
- triangle (1)
- volume (1)
Blog Archive
Link List
About Me
Cari
rss
12.02
Langganan:
Posting Komentar (Atom)
3 komentar:
cara compilenya giaman y??
coba
kok gk jadi??
knp y??
Posting Komentar