Last active
April 30, 2019 07:31
-
-
Save aperkaz/863524a116b152cd478e7247aa2ec748 to your computer and use it in GitHub Desktop.
Code kata 1 - CodeHub
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
const INVALID = -1; | |
const binarySearch = (target, arr) => { | |
const start = 0; | |
const finish = arr.length - 1; | |
const middle = Math.floor((start + finish) / 2); | |
console.log(arr); | |
console.log('middle value: ',arr[middle]); | |
if(arr[middle] === target){ | |
return middle; | |
} | |
if(start >= finish || finish == 0){ | |
return INVALID; | |
} | |
if(arr[middle] > target){ | |
let index = binarySearch(target, arr.slice(0, middle)); | |
console.log('index ',index); | |
return (index !== INVALID)? index : 0 ; | |
}else{ | |
let index = binarySearch(target, arr.slice(middle+1)); | |
console.log('index ',index); | |
return(index !== INVALID)? index + middle+1 : index; | |
} | |
} | |
const arr = [1,3,4,5,6,7,9,11]; | |
const targetValue =6; | |
console.log('RESULT: ',binarySearch(targetValue, arr, 0)); |
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
const binarySearch = (target, orderedArray) => { | |
let start = 0; | |
let finish = orderedArray.length - 1; | |
let middle = Math.floor((start + finish)/2); | |
console.log('target value ', target); | |
while(orderedArray[middle] !== target && start < finish){ | |
console.log('==='); | |
console.log('start index ',start); | |
console.log('finish index',finish); | |
console.log('middle index',middle); | |
console.log('middle value ',orderedArray[middle]); | |
if(orderedArray[middle] > target){ | |
// smaller | |
finish = middle - 1; | |
}else{ | |
// bigger | |
start = middle + 1; | |
} | |
middle = Math.floor((start + finish)/2); | |
} | |
return (orderedArray[middle] === target)? middle : -1; | |
} | |
const arr = [1,3,4,5,6,7,9,11]; | |
const targetValue = 11; | |
console.log(binarySearch(targetValue, arr)); |
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
const binarySearch = (target, start, finish, orderedArray) => { | |
const middle = Math.floor((start + finish) / 2); | |
if(orderedArray[middle] === target){ | |
return middle; | |
} | |
if(start >= finish){ | |
return -1; | |
} | |
if(orderedArray[middle] < target){ | |
return binarySearch(target, middle +1, finish, orderedArray); | |
}else{ | |
return binarySearch(target, start, middle -1, orderedArray); | |
} | |
} | |
const arr = [1,3,4,5,6,7,9,11]; | |
const targetValue = 3; | |
console.log(binarySearch(targetValue, 0, arr.length, arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment