BinarySearch..java

pahami dan cermati…:D


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BinarySearch {
 public static void main(String[] args){
 String[] arr = {"a","b","c","d"};
 List arrt = new ArrayList(Arrays.asList(arr));
 for(int i=0;i<arr.length;i++){
 System.out.print(arr[i] + " ");
 }
 int index = Collections.binarySearch(arrt, "d");
 System.out.println("data ada di urutan ke " + index);
 }
}

example number 2:


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class BinarySearch2 {
 public static void main(String[] args){
 Scanner input = new Scanner(System.in);
 System.out.println("masukan jumlah data ");
 int inpute = input.nextInt();
 String[] jml = new String[inpute];

 for(int i=0;i<inpute;i++){
 System.out.print("Masukan inputan ");
 jml[i] = input.next();
 }
 List ar = new ArrayList(Arrays.asList(jml));
 for(String x : jml){
 System.out.print(x + " ");
 }
 System.out.println();
 //cari data
 System.out.println("Masukkan data yg ingin dicari ");
 String se = input.next();
 int index = Collections.binarySearch(ar, se);

 if(index < 0)
 {
 System.out.println("Data not Found");
 }else{
 System.out.println("data ditemukkan di " + index);
 }

 }
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s