Binary search algorithm for finding a value at specified index in two independent sorted arrays (index = element at specified index of merged, sorted array)
Github repository:
https://github.com/Yuri-Predborskiy/js-fiddles-for-leetcode/tree/master/4.%20Median%20of%20Two%20Sorted%20Arrays
Original code from GeeksForGeeks:
https://ide.geeksforgeeks.org/DJu7to
Original article:
K-th Element of Two Sorted Arrays
https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/
For realization see Javascript tab.
JavaScript
let pass = 0;
let logging = false;
let logs = 0;
function log(...messages) {
if (logging) console.log(messages.join(' '));
}
function getIndexElementInTwoSortedArrays(arrLeft, arrRight, index, startLeft = 0, endLeft = arrLeft.length, startRight = 0, endRight = arrRight.length) {
let shiftLeft = Math.floor((endLeft - startLeft) / 2);
let shiftRight = Math.floor((endRight - startRight) / 2);
log('pass', pass++, 'startLeft', startLeft, 'endLeft', endLeft, 'startRight', startRight, 'endRight', endRight, 'index', index);
if (startLeft === endLeft) {
log('reached end of array 1, element not found, so it should be at index startRight + index in arrRight');
log(`looking at arrRight[${startRight + index}], value is ${arrRight[startRight + index]}`);
return arrRight[startRight + index];
}
if (startRight === endRight) {
log('reached end of array 2, element not found, so it should be at index startLeft + index in arrLeft');
log(`looking at arrLeft[${startLeft + index}], value is ${arrLeft[startLeft + index]}`);
return arrLeft[startLeft + index];
}
if (shiftLeft + shiftRight < index) {
// log(`shiftLeft (${shiftLeft}) + shiftRight (${shiftRight}) < index (${index})`);
log(`mids (${shiftLeft} + ${shiftRight} = ${shiftLeft + shiftRight}) < index (${index}), lets increase start and limit index`);
if (arrLeft[startLeft + shiftLeft] < arrRight[startRight + shiftRight]) {
// log(`arrLeft[startLeft + shiftLeft] ${arrLeft[startLeft + shiftLeft]} < arrRight[startRight + shiftRight] ${arrRight[startRight + shiftRight]}`);
log('limiting start of left and limiting index - shiftLeft - 1');
return getIndexElementInTwoSortedArrays(arrLeft, arrRight, index - shiftLeft - 1, startLeft + shiftLeft + 1, endLeft, startRight, endRight);
} else {
// log(`arrLeft[startLeft + shiftLeft] ${arrLeft[startLeft + shiftLeft]} >=...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.