import java.util.Scanner;
/**
*
* @author USERPC
*/
public class Latihan1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Dekarasi
Scanner a=new Scanner(System.in);
int N,i,x;
//Agoritma
System.out.println("Banyak data :");
N=a.nextInt();
int L[]=new int [N];
for(i=0;i<L.length;i++)
{
System.out.print("Masukkan data :");
L[i]=a.nextInt();
}
System.out.println("Nilai yang dicari :");
x=a.nextInt();
Pbubblesortinc(L);
Pbinarysearch(L,x);
}
public static void Pbubblesortinc(int L[])
{//Deklarasi
int i,j,temp;
//Algoritma
for (i=0;i<L.length;i++)
{for(j=L.length-1;j>=i+1;j--)
{
if (L[j]<L[j-1])
{
temp=L[j];
L[j]=L[j-1];
L[j-1]=temp;
}
}
}
}
public static void Pbinarysearch (int L[], int x)
{ //Deklarasi
int cari,bottom,top,middle;
//Algoritma
cari=0;
bottom=0;
top=L.length-1;
while(top>=bottom)
{
middle=(top+bottom)/2;
if(x==L[middle])
{ cari=1;
System.out.println("DATA DITEMUKAN PADA INDEKS ke- " +middle);
}
if(x<L[middle])
top=middle-1;
else
bottom=middle+1;
}
if (cari==0)
{System.out.println("DATA TIDAK DITEMUKAN");
}
}
}
Latihan 1 (Program pencarian binary data bertipe integer dengan pengurutan
menaik bubble sort menggunakan fungsi Fbinarysearch)
/*
*/
package Binarysearch;
import java.util.Scanner;
/**
* @author USERPC
*/
/**
*/
//Dekarasi
int N,i,x;
//Agoritma
N=a.nextInt();
int L[]=new int [N];
for(i=0;i<L.length;i++)
L[i]=a.nextInt();
x=a.nextInt();
Pbubblesortinc(L);
boolean ketemu=Fbinarysearch(L,x);
if(ketemu==true)
{System.out.println("Data ditemukan");
else
{//Deklarasi
int i,j,temp;
//Algoritma
for (i=0;i<L.length;i++)
{for(j=L.length-1;j>=i+1;j--)
if (L[j]<L[j-1])
temp=L[j];
L[j]=L[j-1];
L[j-1]=temp;
{ //Deklarasi
int bottom,top,middle;
boolean ketemu;
//Algoritma
ketemu=false;
bottom=0;
top=L.length-1;
while(top>=bottom)
middle=(top+bottom)/2;
if(x==L[middle])
ketemu=true;
if(x<L[middle])
top=middle-1;
else
bottom=middle+1;
return ketemu;
}
Latihan 2 (Program pencarian binary data bertipe integer dengan pengurutan
menurun selection sort )
/*
*/
package Binarysearch;
import java.util.Scanner;
/**
* @author USERPC
*/
/**
*/
//Dekarasi
int N,i,x;
//Agoritma
N=a.nextInt();
int L[]=new int [N];
for(i=0;i<L.length;i++)
L[i]=a.nextInt();
x=a.nextInt();
Pselectionsortdec(L);
Pbinarysearch(L,x);
{//Deklarasi
int i,j,temp,tampung;
//Algoritma
for(i=0;i<L.length;i++)
{temp=i;
for(j=i+1;j<L.length;j++)
{if (L[j]>L[temp])
{temp=j;
tampung=L[i];
L[i]=L[temp];
L[temp]=tampung;
}
public static void Pbinarysearch (int L[], int x)
{ //Deklarasi
int cari,bottom,top,middle;
//Algoritma
cari=0;
bottom=0;
top=L.length-1;
while(top>=bottom)
middle=(top+bottom)/2;
if(x==L[middle])
{ cari=1;
if(x<L[middle])
bottom=middle+1;
else
top=middle-1;
if (cari==0)
}
Latihan 3 (Program pencarian binary data bertipe char dengan pengurutan
menurun selection sort )
/*
*/
package Binarysearch;
import java.util.Scanner;
/**
* @author USERPC
*/
/**
*/
//Dekarasi
int N,i;
char x;
//Agoritma
for(i=0;i<L.length;i++)
L[i]=a.next().charAt(0);
x=a.next().charAt(0);
Pselectionsortdec(L);
Pbinarysearch(L,x);
{//Deklarasi
int i,j,temp;
char tampung;
//Algoritma
for(i=0;i<L.length;i++)
{temp=i;
for(j=i+1;j<L.length;j++)
{if (L[j]>L[temp])
{temp=j;
tampung=L[i];
L[i]=L[temp];
L[temp]=tampung;
}
}
{ //Deklarasi
int cari,bottom,top,middle;
//Algoritma
cari=0;
bottom=0;
top=L.length-1;
while(top>=bottom)
middle=(top+bottom)/2;
if(x==L[middle])
{ cari=1;
if(x<L[middle])
bottom=middle+1;
else
top=middle-1;
if (cari==0)
}
Latihan 4 (Program pencarian binary data bertipe String dengan pengurutan
menaik selection sort )
/*
*/
package Binarysearch;
import java.util.Scanner;
/**
* @author USERPC
*/
/**
*/
//Dekarasi
int N,i;
String x;
//Agoritma
for(i=0;i<L.length;i++)
L[i]=a.next();
x=a.next();
Pbubblesortinc(L);
Pbinarysearch(L,x);
{//Deklarasi
int i,j;
String temp;
//Algoritma
for (i=0;i<L.length;i++)
{for(j=L.length-1;j>=i+1;j--)
if (L[j].compareTo(L[j-1])<0)
temp=L[j];
L[j]=L[j-1];
L[j-1]=temp;
}
}
{ //Deklarasi
int bottom,top,middle,cari;
//Algoritma
cari=0;
bottom=0;
top=L.length-1;
while(top>=bottom)
middle=(top+bottom)/2;
if(x.equals(L[middle]))
{ cari=1;
if(x.compareTo(L[middle])<0)
top=middle-1;
else
bottom=middle+1;
if (cari==0)