-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinarysearch.cr
32 lines (26 loc) · 2.6 KB
/
binarysearch.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "../src/nodejs"
code = <<-CODE
const binarySearch = (arr, value) => {
const array = arr.sort((a, b) => { return (a < b ? -1 : 1); });
let guess;
let min = 0;
let max = array.length - 1;
while(min <= max){
guess = Math.floor((min + max) /2);
if(array[guess] === value)
return guess;
else if(array[guess] < value)
min = guess + 1;
else
max = guess - 1;
}
return -1;
}
const arr = [32, 5, 123, 6, 29, 300, 123, 556, 89, 23, 9, 11, 2325, 65, 88,747, 372, 643, 919, 49, 610, 25, 605, 734, 999, 621, 63, 691, 336, 499, 310, 831, 616, 286, 882, 895, 705, 455, 52, 814, 818, 274, 458, 347, 812, 759, 761, 123, 512, 618, 703, 825, 513, 190, 698, 965, 770, 714, 924, 403, 581, 267, 498, 287, 378, 465, 586, 168, 128, 950, 815, 425, 208, 157, 33, 775, 39, 438, 424, 374, 952, 404, 118, 730, 255, 923, 950, 18, 928, 300, 0, 798, 666, 305, 535, 351, 165, 368, 323, 653, 275, 677, 992, 277, 871, 378, 466, 346, 429, 379, 637, 575, 702, 322, 350, 670, 179, 543, 43, 78, 798, 507, 241, 348, 145, 352, 876, 659, 103, 493, 320, 933, 599, 999, 901, 392, 156, 577, 614, 430, 179, 810, 688, 163, 675, 414, 347, 252, 780, 630, 871, 438, 687, 917, 836, 674, 656, 545, 630, 996, 45, 440, 510, 186, 280, 966, 471, 966, 400, 15, 374, 553, 3, 821, 452, 74, 117, 227, 885, 151, 287, 350, 440, 161, 996, 259, 591, 137, 360, 308, 905, 983, 257, 539, 835, 378, 478, 168, 780, 985, 497, 213, 658, 795, 514, 589, 521, 674, 808, 81, 81, 763, 161, 465, 92, 32, 5, 123, 6, 29, 300, 123, 556, 89, 23, 9, 11, 2325, 65, 88,747, 372, 643, 919, 49, 610, 25, 605, 734, 999, 621, 63, 691, 336, 499, 310, 831, 616, 286, 882, 895, 705, 455, 52, 814, 818, 274, 458, 347, 812, 759, 761, 123, 512, 618, 703, 825, 513, 190, 698, 965, 770, 714, 924, 403, 581, 267, 498, 287, 378, 465, 586, 168, 128, 950, 815, 425, 208, 157, 33, 775, 39, 438, 424, 374, 952, 404, 118, 730, 255, 923, 950, 18, 928, 300, 0, 798, 666, 305, 535, 351, 165, 368, 323, 653, 275, 677, 992, 277, 871, 378, 466, 346, 429, 379, 637, 575, 702, 322, 350, 670, 179, 543, 43, 78, 798, 507, 241, 348, 145, 352, 876, 659, 103, 493, 320, 933, 599, 999, 901, 392, 156, 577, 614, 430, 179, 810, 688, 163, 675, 414, 347, 252, 780, 630, 871, 438, 687, 917, 836, 674, 656, 545, 630, 996, 45, 440, 510, 186, 280, 966, 471, 966, 400, 15, 374, 553, 3, 821, 452, 74, 117, 227, 885, 151, 287, 350, 440, 161, 996, 259, 591, 137, 360, 308, 905, 983, 257, 539, 835, 378, 478, 168, 780, 985, 497, 213, 658, 795, 514, 589, 521, 674, 808, 81, 81, 763, 161, 465, 92];
for (let i = 0; i < 5000; i++) {
arr.forEach((value) => {
console.log(binarySearch(arr, value));
});
}
CODE
Nodejs.eval(code)