|
| 1 | +<img src="../img/IDEA%20(1).png"> |
| 2 | +<img src="../img/IDEA%20(2).png"> |
| 3 | + |
| 4 | + |
| 5 | +# Linear Vs Binary - 🔍 Search |
| 6 | + |
| 7 | +```java |
| 8 | +package com.akashdipmahapatra.DSA; |
| 9 | + |
| 10 | +public class A_Search { |
| 11 | + |
| 12 | +public static void main(String[] args){ |
| 13 | + int arr[] = {5, 7, 9, 11, 13}; |
| 14 | + int target = 11; |
| 15 | + |
| 16 | +// Method |
| 17 | + int result_1 = linearSearch(arr, target); |
| 18 | + int result_2 = BinarySearch(arr, target); |
| 19 | + |
| 20 | +// output |
| 21 | + if(result_1 != -1) { |
| 22 | + System.out.println("Element found at Index: " + result_2); |
| 23 | + }else{ |
| 24 | + System.out.println("Element not found"); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | +```java |
| 29 | +public static int linearSearch(int[] arr, int target){ |
| 30 | + int steps = 0; // To count the Steps (Optional) |
| 31 | + |
| 32 | + for(int i = 0; i<= arr.length; i++){ |
| 33 | + steps++; |
| 34 | + if(arr[i] == target){ |
| 35 | + System.out.println("Steps taken is Linear Search: " + steps); |
| 36 | + return i; |
| 37 | + } |
| 38 | + } |
| 39 | + return -1; |
| 40 | +} |
| 41 | +``` |
| 42 | +```java |
| 43 | +public static int BinarySearch(int[] arr, int target){ |
| 44 | +// 5, 7, 9, 11, 13 |
| 45 | + |
| 46 | + int steps = 0; // Optional |
| 47 | + int left = 0; |
| 48 | + int right = arr.length-1; |
| 49 | + |
| 50 | + while(left <= right){ |
| 51 | + steps++; |
| 52 | + int mid = (left + right)/2; |
| 53 | + |
| 54 | + if(arr[mid] == target){ |
| 55 | + System.out.println("Steps taken is Binary Search: " + steps); |
| 56 | + return mid; |
| 57 | + }else if(arr[mid] < target){ |
| 58 | + left = mid+1; |
| 59 | + }else{ |
| 60 | + right = mid-1; |
| 61 | + } |
| 62 | + } |
| 63 | + System.out.println("Steps taken is Binary Search: " + steps); // To cover all conditions. |
| 64 | + return -1; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## ❌ For loop not allow for "Divide-and-conquer" like Binary Search, Quick Sort, Marge Sort etc. |
| 69 | + |
| 70 | +```java |
| 71 | +// For loop not allow for "Divide-and-conquer" like Binary Search, Quick Sort, Marge Sort etc. |
| 72 | + |
| 73 | +// int left = 0; |
| 74 | +// int right = arr.length-1; |
| 75 | +// |
| 76 | +// for(int i = left; i < right; i++){ |
| 77 | +// int mid = (left + right)/2; |
| 78 | +// |
| 79 | +// if(arr[mid] == target){ |
| 80 | +// return mid; |
| 81 | +// }else if(arr[mid] < target){ |
| 82 | +// left = mid + 1; |
| 83 | +// }else{ |
| 84 | +// right = mid - 1; |
| 85 | +// } |
| 86 | +// } |
| 87 | +// return -1; |
| 88 | +//} |
| 89 | + |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Linear Vs Binary - 🔍 Search |
| 94 | + |
| 95 | +<img src="../img/JAVA%20Search%20(1).png"> |
| 96 | +<img src="../img/JAVA%20Search%20(2).png"> |
| 97 | +<img src="../img/JAVA%20Search%20(3).png"> |
| 98 | +<img src="../img/JAVA%20Search%20(4).png"> |
| 99 | +<img src="../img/JAVA%20Search%20(5).png"> |
0 commit comments