You are on page 1of 15

Latihan 1 (Program pencarian binary data bertipe integer dengan pengurutan

menaik bubble sort menggunakan procedure Pbinarysearch)


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Binarysearch;

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)
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Binarysearch;

import java.util.Scanner;

/**

* @author USERPC

*/

public class NewMain {

/**

* @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);

boolean ketemu=Fbinarysearch(L,x);

if(ketemu==true)

{System.out.println("Data ditemukan");

else

System.out.println("Data tidak ditemukan");

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 boolean Fbinarysearch (int L[], int x)

{ //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 )
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Binarysearch;

import java.util.Scanner;

/**

* @author USERPC

*/

public class Latihan2 {

/**

* @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();

Pselectionsortdec(L);

Pbinarysearch(L,x);

public static void Pselectionsortdec(int L[])

{//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;

System.out.println("DATA DITEMUKAN PADA INDEKS ke- " +middle);

if(x<L[middle])

bottom=middle+1;

else

top=middle-1;

if (cari==0)

{System.out.println("DATA TIDAK DITEMUKAN");

}
Latihan 3 (Program pencarian binary data bertipe char dengan pengurutan
menurun selection sort )
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Binarysearch;

import java.util.Scanner;

/**

* @author USERPC

*/

public class Latihan3 {

/**

* @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;

char x;

//Agoritma

System.out.println("Banyak data :");


N=a.nextInt();

char L[]=new char [N];

for(i=0;i<L.length;i++)

System.out.print("Masukkan data :");

L[i]=a.next().charAt(0);

System.out.println("Nilai yang dicari :");

x=a.next().charAt(0);

Pselectionsortdec(L);

Pbinarysearch(L,x);

public static void Pselectionsortdec(char L[])

{//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;

}
}

public static void Pbinarysearch (char L[], char 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])

bottom=middle+1;

else

top=middle-1;

if (cari==0)

{System.out.println("DATA TIDAK DITEMUKAN");

}
Latihan 4 (Program pencarian binary data bertipe String dengan pengurutan
menaik selection sort )
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Binarysearch;

import java.util.Scanner;

/**

* @author USERPC

*/

public class Latihan4 {

/**

* @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;

String x;

//Agoritma

System.out.println("Banyak data :");


N=a.nextInt();

String L[]=new String [N];

for(i=0;i<L.length;i++)

System.out.print("Masukkan data :");

L[i]=a.next();

System.out.println("Nilai yang dicari :");

x=a.next();

Pbubblesortinc(L);

Pbinarysearch(L,x);

public static void Pbubblesortinc(String L[])

{//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;

}
}

public static void Pbinarysearch (String L[], String x)

{ //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;

System.out.println("DATA DITEMUKAN PADA INDEKS ke-" +middle);}

if(x.compareTo(L[middle])<0)

top=middle-1;

else

bottom=middle+1;

if (cari==0)

{System.out.println("DATA TIDAK DITEMUKAN");

You might also like