Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Q23 linear search
- import java.util.*;
- public class Main
- {
- public static void main(String[] args) {
- Scanner sc= new Scanner(System.in);
- int[] arr = {1,2,3,4,5,6,7,9};
- System.out.println("Enter the number");
- int n = sc.nextInt(),check=0;
- for(int i=0 ; i<8 ; i++){
- if(arr[i]==n){
- check=1;
- break;
- }
- }
- if(check==1){
- System.out.println("Number found");
- }
- else{
- System.out.println("Not found");
- }
- }
- }
- /*
- Enter the number 0
- Not found
- */
- Q 24 Binary search
- // Q21 Maximum element of array
- import java.util.*;
- public class Main
- {
- static boolean Binary_Search(int arr[] , int find , int n)
- {
- int low = 0 , high = n-1 , mid = (high + low) / 2, check=0;
- while(low<=high){
- if(arr[mid] == find){
- return true;
- }
- else if(find > arr[mid]){
- low = mid + 1;
- }
- else{
- high = mid - 1;
- }
- mid = (high + low) /2;
- }
- return false;
- }
- public static void main(String[] args) {
- Scanner sc= new Scanner(System.in);
- int[] arr = {1,2,3,4,5,6,7,9};
- System.out.println("Enter the number");
- int n = sc.nextInt();
- if( Binary_Search(arr,n,8) ){
- System.out.println("Number found");
- }
- else{
- System.out.println("Not found");
- }
- }
- }
- /*
- Enter the number 3
- Number found
- */
Add Comment
Please, Sign In to add comment