nie-ii-year

lab stuff from undergrad second year.
git clone http://git.hanabi.in/repos/nie-ii-year.git
Log | Files | Refs | LICENSE

commit 3f5f19f498d93cd2c354f65803af445ea65b69d1
parent 3ab98266dcd39b857c1143e54837a12492668908
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Wed, 31 Jan 2018 08:17:43 +0530

Create recursive.js
Diffstat:
As4/algorithms/binarySearch/recursive.js | 39+++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)

diff --git a/s4/algorithms/binarySearch/recursive.js b/s4/algorithms/binarySearch/recursive.js @@ -0,0 +1,39 @@ +function binarySearch(key, arr, low = 0, high = arr.length - 1){ + if(low > high) + return -1; + mid = (low + high) >> 1; + if(arr[mid] == key) + return mid; + if(arr[mid] < key) + return binarySearch(key, arr, mid + 1, high); + else + return binarySearch(key, arr, low, mid - 1); +} + +var size; +do{ + print("Enter array size: "); + size = readline(); + size = parseInt(size); + if(size % 1 !== 0 || size < 1) + print("Invalid size, try again."); +}while (size % 1 !== 0 || size < 1); + +array = []; +array.length = size; +print("Enter array elements: "); + +for(var i = 0; i < size; i++) + array[i] = readline(); + +array.sort(function(a, b){return a - b;}); + +print("Enter key to be searched: "); +key = readline(); + +location = binarySearch(key, array); + +if(location > -1) + print(key + " was found at: " + location); +else + print(key + " was not found.");