forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
38 lines (30 loc) · 1.18 KB
/
Copy pathcode.js
File metadata and controls
38 lines (30 loc) · 1.18 KB
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
33
34
35
36
37
38
function BinarySearch(array, element, minIndex, maxIndex) { // array = sorted array, element = element to be found, minIndex = minIndex index, maxIndex = maxIndex index
if (minIndex > maxIndex) {
tracer._print(element + ' is not found!');
return -1;
}
var middleIndex = Math.floor((minIndex + maxIndex) / 2);
var testElement = array[middleIndex];
tracer._print('Searching at index: ' + middleIndex);
tracer._notify(middleIndex);
if (testElement < element) {
tracer._print('Going right.');
return BinarySearch(array, element, middleIndex + 1, maxIndex);
}
if (testElement > element) {
tracer._print('Going left.');
return BinarySearch(array, element, minIndex, middleIndex - 1);
}
if (testElement === element) {
tracer._print(element + ' is found at position ' + middleIndex + '!');
tracer._select(middleIndex);
return middleIndex;
}
tracer._print(element + ' is not found!');
return -1;
}
var element = D[Math.random() * D.length | 0];
tracer._sleep(1000);
tracer._pace(1000);
tracer._print('Using binary search to find ' + element);
BinarySearch(D, element, 0, D.length - 1);