Skip to content

This PR implements the code for exponential search in javascript referred in issue #42. #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed ClassicalAlgos/dijkstra's algo/dijkstra.exe
Binary file not shown.
14 changes: 14 additions & 0 deletions searchingAlgo/exponentialSearch/exponentialsearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
async function exponentialSearch(a, k, display) {
let i = 1;
while (i<a.length && a[i]<=k) {
i*=2;

glob_comp+=2;
refresh(glob_comp);

display(a, undefined, 0, i);
await sleep(glob_sleep_time);
}

let re = await binarySearch(a, k, Math.floor(i/2), i, display);
}
51 changes: 51 additions & 0 deletions searchingAlgo/fibonacciSearch/FibonacciSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function fib(n) {
if (n <= 0)
return 0;
if (n <= 2)
return 1;
return fib(n-1) + fib(n-2);
}

function smallest_greater_eq_fib(n) {
let f = fib(0),
cu = 0;
while (f < n)
f = fib(++cu);

return cu;
}

async function fibonacciSearch(a, k, l, r, display) {
let f = smallest_greater_eq_fib(r-l+1);

while (f >= 0) {
i = Math.min(l+fib(f-1), r-1);
i = Math.max(0, i);

refresh(glob_comp, a[i]);

display(a, i, l, r);
await sleep(glob_sleep_time);

if (a[i]==k) {
glob_comp++;
refresh(glob_comp, a[i]);

return new Promise(resolve => resolve(i));
} else if (k < a[l+fib(f-1)]) {
glob_comp+=2;
refresh(glob_comp, a[i]);

r = i;
f-=1;
} else {
glob_comp+=2;
refresh(glob_comp, a[i]);

l = i;
f-=2;
}
}

return new Promise(resolve => resolve(-1));
}