Professional Documents
Culture Documents
LAB CAT 1
5. Write a method to implement the binary search. Use a package that has
the class sort with a method void bubble_sort(double[] array) to perform a
bubble sort public void search(double[] array) .
Write a test program that prompts the user to enter n numbers, and a search
element. Invokes this method to return the position of the search element.
Code:
//package used
package sortingpkg;
}
}
package labcat;
import sortingpkg.*;
import java.util.Scanner;
m=(l+u)/2;
if(num==arr[m]) {
System.out.println("Number found at " + (m+1) + " position.");
key=true;
break;
}
if(num>arr[m])
l=m+1;
else
u=m-1;
}
if(!key) {
System.out.println("Number not found!!");
}
}
double element;
System.out.println("Enter the element to be searched:");
element= sc.nextDouble();
Output: