public class SearchBinary {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 10, 15, 5, 67, 3, 18, 20 };
SearchBinary search = new SearchBinary();
search.bubble(a);
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+",");
}
System.out.println();
System.out.println(search.binary(a, 34));
System.out.println(search.binary(a, 67));
}
public boolean binary(int[] a,int key)
{
int low = 0;
int high = a.length-1;
int mid;
while(low<=high)
{
mid = (low+high)/2;
if(a[mid] == key)
{
return true;
}
else if(a[mid]>key)
{
high = mid -1;
}
else if(a[mid]<key)
{
low = mid+1;
}
}
return false;
}
public void bubble(int[] a) {
for (int i = 0; i <= a.length-1; i++) {
for (int j=0;j<=i;j++) {
if (a[i] < a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 10, 15, 5, 67, 3, 18, 20 };
SearchBinary search = new SearchBinary();
search.bubble(a);
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+",");
}
System.out.println();
System.out.println(search.binary(a, 34));
System.out.println(search.binary(a, 67));
}
public boolean binary(int[] a,int key)
{
int low = 0;
int high = a.length-1;
int mid;
while(low<=high)
{
mid = (low+high)/2;
if(a[mid] == key)
{
return true;
}
else if(a[mid]>key)
{
high = mid -1;
}
else if(a[mid]<key)
{
low = mid+1;
}
}
return false;
}
public void bubble(int[] a) {
for (int i = 0; i <= a.length-1; i++) {
for (int j=0;j<=i;j++) {
if (a[i] < a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}