function binarySearch(numbers, element) { //time complexity is log(n) it excecutes very fast than linear search n=arraylength; return search(numbers,element,0,numbers.length-1)} function search(numbers,element,left,right){ if(left>right) return -1; let pivot =Math.floor((left+right)/2) if(numbers[pivot]===element) return pivot if(numbers[pivot]>element) return search(numbers,element,left,pivot-1) return search(numbers,element,pivot+1,right) }
- function binarySearch(numbers, element) {
- //time complexity is log(n) it excecutes very fast than linear search n=arraylength;
let left = 0;let right = numbers.length - 1;while(left <= right) {let pivot = Math.floor((left + right) / 2);- return search(numbers,element,0,numbers.length-1)}
- function search(numbers,element,left,right){
- if(left>right) return -1;
- let pivot =Math.floor((left+right)/2)
- if(numbers[pivot]===element) return pivot
- if(numbers[pivot]>element) return search(numbers,element,left,pivot-1)
- return search(numbers,element,pivot+1,right)
if(numbers[pivot]==element){return pivot;}else if(numbers[pivot]>element){right=pivot-1;}else{left=pivot+1;}}return -1;- }
function binarySearch(numbers, element) { //time complexity is log(n) it excecutes very fast than linear search n=arraylength; let left = 0; let right = numbers.length - 1; while(left <= right) { let pivot = Math.floor((left + right) / 2); if(numbers[pivot]==element){ return pivot; } else if(numbers[pivot]>element){ right=pivot-1; } else{ left=pivot+1; } } return -1; }
- function binarySearch(numbers, element) {
- //time complexity is log(n) it excecutes very fast than linear search n=arraylength;
- let left = 0;
- let right = numbers.length - 1;
- while(left <= right) {
- let pivot = Math.floor((left + right) / 2);
- if(numbers[pivot]==element){
- return pivot;
- }
- else if(numbers[pivot]>element){
- right=pivot-1;
- }
- else{
- left=pivot+1;
- }
- }
- return -1;
- }
function binarySearch(numbers, element) { let left = 0; let right = numbers.length - 1; while(left <= right) { let pivot = Math.floor((left + right) / 2); if(numbers[pivot]==element){ return pivot; } else if(numbers[pivot]>element){ right=pivot-1; } else{ left=pivot+1; } } return -1; }
- function binarySearch(numbers, element) {
- let left = 0;
- let right = numbers.length - 1;
let pivot = Math.floor((left + right) / 2);while(left < pivot && pivot < right) {if(element < numbers[pivot]) {right = pivot;} else {left = pivot;- while(left <= right) {
- let pivot = Math.floor((left + right) / 2);
- if(numbers[pivot]==element){
- return pivot;
- }
pivot = Math.floor((left + right) / 2);}if(element === numbers[pivot]) {return pivot;}- else if(numbers[pivot]>element){
- right=pivot-1;
- }
- else{
- left=pivot+1;
- }
- }
- return -1;
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("return index of searching element", function() { assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 4), 3); assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 2), 1); assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 8), -1); assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7); }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("return index of searching element", function() {
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 4), 3);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 2), 1);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 8), -1);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7);
- });
- });