|
| 1 | +# Searching in JavaScript |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://youtu.be/7aLKVYpYugQ"> |
| 5 | + <img src="https://img.youtube.com/vi/7aLKVYpYugQ/0.jpg" alt="Searching Algorithms in JavaScript" /> |
| 6 | + </a> |
| 7 | +</p> |
| 8 | + |
| 9 | +### Linear Search in JavaScript |
| 10 | + |
| 11 | +```javascript |
| 12 | +const arr = [1, 2, 6, 9, 0, -5]; |
| 13 | + |
| 14 | +const linearSearch = (arr, target) => { |
| 15 | + for (let i = 0; i < arr.length; i++) { |
| 16 | + if (arr[i] === target) { |
| 17 | + return i; |
| 18 | + } |
| 19 | + } |
| 20 | + return -1; |
| 21 | +} |
| 22 | + |
| 23 | +console.log(linearSearch(arr, 8)); |
| 24 | +console.log(arr.includes(9)); |
| 25 | +console.log(arr.indexOf(9)); |
| 26 | +console.log(arr.find((num) => num > 0)); |
| 27 | +console.log(arr.findIndex((num) => num < 0)); |
| 28 | +``` |
| 29 | + |
| 30 | +### Binary Search In JavaScript |
| 31 | + |
| 32 | +```javascript |
| 33 | +const BinarySearch = (arr, target) => { |
| 34 | + let start = 0, end = arr.length - 1; |
| 35 | + while (start <= end) { |
| 36 | + let mid = Math.floor((start + end) / 2); |
| 37 | + |
| 38 | + if (arr[mid] === target) { |
| 39 | + return mid; |
| 40 | + } |
| 41 | + |
| 42 | + else if (arr[mid] > target) { |
| 43 | + end = mid - 1; |
| 44 | + } |
| 45 | + |
| 46 | + else { |
| 47 | + start = mid + 1; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return -1; |
| 52 | +} |
| 53 | + |
| 54 | +console.log(BinarySearch([1, 4, 6, 9, 12, 15], 8)); |
| 55 | +``` |
| 56 | + |
| 57 | +### Binary Search using Recursion |
| 58 | + |
| 59 | +```javascript |
| 60 | +const BinarySearchRecur = (arr, target) => { |
| 61 | + return BinarySearchUtil(arr, target, 0, arr.length); |
| 62 | +} |
| 63 | + |
| 64 | +const BinarySearchUtil = (arr, target, start, end) => { |
| 65 | + if (start > end) |
| 66 | + return -1; |
| 67 | + |
| 68 | + let mid = Math.floor((start + end) / 2); |
| 69 | + |
| 70 | + if (arr[mid] === target) { |
| 71 | + return mid; |
| 72 | + } |
| 73 | + |
| 74 | + else if (arr[mid] > target) { |
| 75 | + return BinarySearchUtil(arr, target, start, mid - 1); |
| 76 | + } |
| 77 | + |
| 78 | + else { |
| 79 | + return BinarySearchUtil(arr, target, mid + 1, end); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Find floor and ceil value of X in an array |
| 85 | + |
| 86 | +```javascript |
| 87 | +const floorCeil = (arr, target) => { |
| 88 | + let start = 0, end = arr.length; |
| 89 | + let floor = -1, ceil = -1; |
| 90 | + while(start <= end){ |
| 91 | + let mid = Math.floor((start + end)/2); |
| 92 | + |
| 93 | + if(arr[mid] === target){ |
| 94 | + floor = mid; |
| 95 | + ceil = mid; |
| 96 | + return [ceil, mid] |
| 97 | + } |
| 98 | + |
| 99 | + else if(arr[mid] > target){ |
| 100 | + ceil = mid; |
| 101 | + end = mid - 1; |
| 102 | + } |
| 103 | + |
| 104 | + else { |
| 105 | + floor = mid; |
| 106 | + start = mid + 1; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return [ceil, floor] |
| 111 | +} |
| 112 | + |
| 113 | +## Practice Questions |
| 114 | + |
| 115 | +### Level 1 |
| 116 | +- [Sqrt(x)](https://leetcode.com/problems/sqrtx/) |
| 117 | +- [First Bad Version](https://leetcode.com/problems/first-bad-version) |
| 118 | +- [Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/) |
| 119 | +- [Binary Search](https://leetcode.com/problems/binary-search) |
| 120 | +- [Search Insert Position](https://leetcode.com/problems/search-insert-position) |
| 121 | +- [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array) |
| 122 | + |
| 123 | +### Level 2 |
| 124 | +- [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array) |
| 125 | +- [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) |
| 126 | +- [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) |
| 127 | +- [Find Peak Element](https://leetcode.com/problems/find-peak-element) |
| 128 | +- [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array) |
0 commit comments