Skip to content

Commit 3d9b5c5

Browse files
committed
binary search
1 parent 43d2478 commit 3d9b5c5

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
var binarySearch = function(arr, valueToSearch, lowerIndex, upperIndex) {
3+
if (lowerIndex > upperIndex) {
4+
return -1;
5+
}
6+
const mid = parseInt((upperIndex + lowerIndex)/2);
7+
if (valueToSearch === arr[mid]) {
8+
console.log('found at ' + mid);
9+
return mid;
10+
}
11+
if (valueToSearch > arr[mid]) {
12+
lowerIndex = mid + 1;
13+
return binarySearch(arr, valueToSearch, lowerIndex, upperIndex);
14+
} else {
15+
upperIndex = mid - 1;
16+
return binarySearch(arr, valueToSearch, lowerIndex, upperIndex);
17+
}
18+
19+
}
20+
21+
const arr = [6, 8, 19, 20, 23, 41, 49, 53, 56];
22+
let indexOfValue = binarySearch(arr, 6, 0, arr.length - 1);
23+
console.log(indexOfValue, 'index of value');
24+
module.exports = binarySearch;

searchAlgrothems/quickSort/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
// O (n 2) worst case
22

3-
function swap(a ,b) {
4-
const temp = a;
5-
a = b;
6-
b = temp;
7-
}
8-
93
function partition(arr, first, last) {
104
const pivot = first;
115
let lowerIndex = first + 1;

0 commit comments

Comments
 (0)