Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf665f9e

Browse files
committed
make binary_search easier to understand
1 parent9d6a326 commitf665f9e

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

‎algorithm/search/binary_search/iterative/code.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
functionBinarySearch(array,element){// array = sorted array, element = element to be found,
2-
1+
functionBinarySearch(array,element){// array = sorted array, element = element to be found
32
varminIndex=0;
43
varmaxIndex=array.length-1;
5-
varcurrentIndex;
64
vartestElement;
75

86
while(minIndex<=maxIndex){
97

10-
middleIndex=Math.floor((minIndex+maxIndex)/2);
8+
varmiddleIndex=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‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ function BinarySearch(array, element, minIndex, maxIndex) { // array = sorted ar
88
vartestElement=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)

[8]ページ先頭

©2009-2025 Movatter.jp