Skip to content

Commit

Permalink
Add day 29
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 29, 2019
1 parent 9faa679 commit d00d4ee
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -52,6 +52,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
26. [Day 26 -- Spiral Generation and Copy](./day26) -- [http://codetoexpress.tech/dc/day26/](http://codetoexpress.tech/dc/day26/)
27. [Day 27 -- The Minesweeper Problem](./day27) -- [http://codetoexpress.tech/dc/day27/](http://codetoexpress.tech/dc/day27/)
28. [Day 28 -- Linear Search](./day28) -- [http://codetoexpress.tech/dc/day28/](http://codetoexpress.tech/dc/day28/)
29. [Day 29 -- Linear Search](./day29) -- [http://codetoexpress.tech/dc/day29/](http://codetoexpress.tech/dc/day29/)

## [More Problems](./BONUS/README.md)

Expand Down
File renamed without changes
20 changes: 20 additions & 0 deletions day29/JavaScript/binary.js
@@ -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
50 changes: 50 additions & 0 deletions day29/README.md
@@ -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));
```
Binary file added day29/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day29/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d00d4ee

Please sign in to comment.