style cleanup - mbcs

This commit is contained in:
Dmitry Shirokov 2013-08-14 11:48:54 +10:00
parent 26054b56e7
commit 95fd5ad250
1 changed files with 7 additions and 7 deletions

View File

@ -6,22 +6,22 @@ var util = require('util'),
*/ */
function binarySearch(arr, searchValue) { function binarySearch(arr, searchValue) {
function find(arr, searchValue, left, right) { function find(arr, searchValue, left, right) {
if (right < left) { if (right < left)
return -1; return -1;
}
/* /*
int mid = mid = (left + right) / 2; int mid = mid = (left + right) / 2;
There is a bug in the above line; There is a bug in the above line;
Joshua Bloch suggests the following replacement: Joshua Bloch suggests the following replacement:
*/ */
var mid = Math.floor((left + right) >>> 1); var mid = Math.floor((left + right) >>> 1);
if (searchValue > arr[mid]) { if (searchValue > arr[mid])
return find(arr, searchValue, mid + 1, right); return find(arr, searchValue, mid + 1, right);
} else if (searchValue < arr[mid]) {
if (searchValue < arr[mid])
return find(arr, searchValue, left, mid - 1); return find(arr, searchValue, left, mid - 1);
} else {
return mid; return mid;
}
}; };
return find(arr, searchValue, 0, arr.length - 1); return find(arr, searchValue, 0, arr.length - 1);