Skip to content

Commit f665f9e

Browse files
committed
make binary_search easier to understand
1 parent 9d6a326 commit f665f9e

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

algorithm/search/binary_search/iterative/code.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
function BinarySearch(array, element) { // array = sorted array, element = element to be found,
2-
1+
function BinarySearch(array, element) { // array = sorted array, element = element to be found
32
var minIndex = 0;
43
var maxIndex = array.length - 1;
5-
var currentIndex;
64
var testElement;
75

86
while (minIndex <= maxIndex) {
97

10-
middleIndex = Math.floor((minIndex + maxIndex) / 2);
8+
var middleIndex = Math.floor((minIndex + maxIndex) / 2);
119
testElement = array[middleIndex];
1210

1311
tracer._print('Searching at index: ' + middleIndex);
12+
tracer._selectSet([minIndex, maxIndex]);
1413
tracer._notify(middleIndex);
14+
tracer._deselectSet([minIndex, maxIndex]);
1515

1616
if (testElement < element) {
1717

algorithm/search/binary_search/recursive/code.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ function BinarySearch(array, element, minIndex, maxIndex) { // array = sorted ar
88
var testElement = array[middleIndex];
99

1010
tracer._print('Searching at index: ' + middleIndex);
11+
tracer._selectSet([minIndex, maxIndex]);
1112
tracer._notify(middleIndex);
13+
tracer._deselectSet([minIndex, maxIndex]);
1214

1315
if (testElement < element) {
1416
tracer._print('Going right.');

0 commit comments

Comments
 (0)