33
loading...
This website collects cookies to deliver better user experience
Do you want to understand binary search? Read this article, I have discussed binary search problems in JavaScript.
Divide your array in set of TRUE and FALSE values then find the First occurrence of TRUE or the last occurrence of TRUE. Binary Search Array should be partially false and partially true. We need to find the boundary between them.
function binarySearch(array, data) {
let low = 0;
let high = array.length - 1;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
if (data === array[mid]) return mid;
else if (data < array[mid]) high = mid - 1;
else low = mid + 1;
}
return -1;
}
function isSquare(x) {}
describe('IsSquare', () => {
it('should work correctly #1', () => {
expect(isSquare(16)).toBeTruthy();
});
it('should work correctly #2', () => {
expect(isSquare(6570204424081)).toBeFalsy();
});
});
function isSquare(x) {
// For Array the maximum length is 2GB-1 (2^32-1)
if (x >= Math.pow(2, 32)) return false;
const array = Array.from(Array(Math.floor(x / 2)).keys());
let low = 0;
let high = array.length - 1;
let mid;
let square;
while (low <= high) {
mid = low + Math.floor((high - low) / 2);
square = Math.pow(array[mid], 2);
if (x === square) return true;
else if (x < square) high = mid - 1;
else low = mid + 1;
}
return false;
}
function sqrt(x) {}
describe('Find Square Root of x', () => {
it('should work correctly #1', () => {
expect(sqrt(4)).toBe(2);
});
it('should work correctly #2', () => {
expect(sqrt(36)).toBe(6);
});
});
function sqrt(x) {
var array = Array.from(Array(x).keys());
let low = 0;
let high = array.length - 1;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
let square = array[mid] * array[mid];
if (square === x) {
return mid;
} else if (square > x) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
function findFirstValue(array, data) {}
describe('Find First Value >= x in sorted array', () => {
it('should work correctly #1', () => {
expect(findFirstValue([2, 3, 5, 6, 8, 10, 12], 4)).toBe(5);
});
});
function findFirstValue(array, data) {
let low = 0;
let high = array.length - 1;
let mid;
let result = -1;
while (low <= high) {
mid = low + Math.floor((high - low) / 2);
if (array[mid] >= data) {
result = array[mid];
high = mid - 1; // since I need first occurrence so keep searching in the left sorted half of the array.
} else low = mid + 1;
}
return result;
}
function findMinInCircularlySortedArray(array) {}
describe('Find Min in Circularly Sorted Array', () => {
it('should work correctly #1', () => {
expect(findMinInCircularlySortedArray([11, 12, 15, 18, 2, 5, 6, 8])).toBe(
2
);
});
it('should work correctly #2', () => {
expect(findMinInCircularlySortedArray([6, 7, 9, 15, 19, 2, 3])).toBe(2);
});
});
function findMinInCircularlySortedArray(array) {
let low = 0;
let length = array.length;
let high = length - 1;
let mid;
let result = -1;
while (low <= high) {
mid = low + Math.floor((high - low) / 2);
if (array[mid] <= array[length - 1]) {
result = array[mid];
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}
function findMaximumInIncreasingDecreasingArray(array) {}
describe('find Maximum In Increasing and Decreasing Array', () => {
it('should work correctly #1', () => {
expect(
findMaximumInIncreasingDecreasingArray([
2,
3,
4,
6,
9,
12,
11,
8,
6,
4,
1,
])
).toBe(12);
});
});
function findMaximumInIncreasingDecreasingArray(array) {
let low = 0;
let high = array.length - 1;
let mid;
let answer = -1;
while (low <= high) {
mid = low + Math.floor((high - low) / 2);
if (array[mid] >= array[mid - 1] || mid === 0) {
answer = array[mid];
low = mid + 1;
} else {
high = mid - 1;
}
}
return answer;
}
Rupesh
and you can ask doubts/questions and get more help, tips and tricks.Your bright future is awaiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a new Software Developer, Architect or Lead Engineer role.