/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mp3;
import javax.swing.JOptionPane;
class cari{
int low,high,middle;
int data[] = {9,8,1,6,10,4,3};
int n = 8;
int key = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Nilai Yang ingin Dicari"));
int cari(){
low = 0;
high= n-1;
while (low <= high)
{ middle = (low + high)/2;
if (key == data[middle])
return(middle);
if (key < data[middle])
high = middle - 1;
else
low = middle + 1;
}
return(-1);
}
}
public class sorting {
public static void main(String[] args){
int x[] = {9,8,1,6,10,4,3};
int n = 7;
for (int i = n-1; i>0; i--){
for (int j = 0; j<=i; j++){
if (x[j]>x[i]){
int car,var;
car = x[i];
var = x[j];
x[i] = var;
x[j] = car;
}}
}
System.out.println(x[0]);
System.out.println(x[1]);
System.out.println(x[2]);
System.out.println(x[3]);
System.out.println(x[4]);
System.out.println(x[5]);
System.out.println(x[6]);
int low,high,middle,key;
key = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Nilai Yang ingin Dicari"));
for (int k = n-1; k >0; k--){
if (key == x[k]){
System.out.println(k);
}
}
cari c1;
c1 = new cari();
System.out.println(c1.cari());
}
}
maka contoh output akan menghasilkan :
run:
1
3
4
6
8
9
10
3
3
BUILD SUCCESSFUL (total time: 4 seconds)
No comments:
Post a Comment