add code for chapters 1 and 2 in ts (#102)

This commit is contained in:
Alex
2019-03-28 23:51:48 +02:00
committed by Aditya Bhargava
parent d77cde9e67
commit c745f5d2a7
2 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
function binary_search<T>(list: T[], item: T): number | null {
let low: number = 0;
let high: number = list.length - 1;
while (low <= high) {
let mid: number = Math.floor((low + high) / 2);
let guess: T = list[mid];
  if (guess === item) {
return mid;
}
if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
const my_list = [1, 3, 5, 7, 9];
console.log(binary_search(my_list, 3)); // 1
console.log(binary_search(my_list, -1)); // null