Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9faa679
commit d00d4ee
Showing
6 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function binarySearch(arr, elem) { | ||
var start = 0; | ||
var end = arr.length - 1; | ||
var middle = Math.floor((start + end) / 2); | ||
while(arr[middle] !== elem && start <= end) { | ||
if(elem < arr[middle]){ | ||
end = middle - 1; | ||
} else { | ||
start = middle + 1; | ||
} | ||
middle = Math.floor((start + end) / 2); | ||
} | ||
if(arr[middle] === elem){ | ||
return middle; | ||
} | ||
return undefined; | ||
} | ||
|
||
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8)); // 5 | ||
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7)); // undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
![cover](./cover.png) | ||
|
||
# Day 28 - Search and Sort Algorithms Part B: The Binary Search | ||
|
||
Binary Search is searching technique which works on Divide and Conquer approach. Indeed an efficient searching algorithm with a runtime of `O(log(n))`, but it works only on sorted arrays. | ||
|
||
## Question | ||
|
||
Given a sorted array, and a number n, write a program to implement binary search and finid the index of the given number. | ||
|
||
Try to do it using recursion as well as iteration. | ||
|
||
**Example** | ||
|
||
``` | ||
input: arr = [1, 2, 3, 4, 5, 8, 9], num = 8 | ||
output: 5 (index of found element) | ||
input: arr = [1, 2, 3, 4, 5, 8, 9], num = 7 | ||
output: undefined | ||
``` | ||
|
||
![ques](./ques.png) | ||
|
||
## Solution | ||
|
||
### [JavaScript Implementation](./JavaScript/binary.js) | ||
|
||
```js | ||
function binarySearch(arr, elem) { | ||
var start = 0; | ||
var end = arr.length - 1; | ||
var middle = Math.floor((start + end) / 2); | ||
while(arr[middle] !== elem && start <= end) { | ||
if(elem < arr[middle]){ | ||
end = middle - 1; | ||
} else { | ||
start = middle + 1; | ||
} | ||
middle = Math.floor((start + end) / 2); | ||
} | ||
if(arr[middle] === elem){ | ||
return middle; | ||
} | ||
return undefined; | ||
} | ||
|
||
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8)); | ||
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7)); | ||
``` |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.