forked from posabsolute/javascript-binary-search-algorithm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cedric Dugas
committed
Nov 12, 2010
0 parents
commit 812f4b5
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | ||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | ||
<title>index</title> | ||
<script src="array.js" type="text/javascript" charset="utf-8"></script> | ||
|
||
<script src="searchNormal.js" type="text/javascript" charset="utf-8"></script> | ||
<script src="searchBinary.js" type="text/javascript" charset="utf-8"></script> | ||
</head> | ||
<body> | ||
<script type="text/javascript"> | ||
|
||
var testNormal = searchNormal("s", array2, true) | ||
var testBinary = searchBinary("s", array2, true) | ||
|
||
</script> | ||
var testNormal = searchNormal("s", array2, true)<br /> | ||
var testBinary = searchBinary("s", array2, true) | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Javascript string localization | ||
|
||
A small wrapper that handle global string localization in your javascript files. | ||
|
||
for more information: http://www.position-absolute.com/articles/managing-string-localization-in-javascript-files/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
|
||
var searchBinary = function(needle, haystack, case_insensitive) { | ||
if(needle == "") return []; | ||
var haystackLength = haystack.length; | ||
var letterNumber = needle.length; | ||
case_insensitive = (typeof(case_insensitive) === 'undefined' || case_insensitive) ? true:false; | ||
needle = (case_insensitive) ? needle.toLowerCase():needle; | ||
|
||
/* start binary search, Get middle position */ | ||
var getElementPosition = findElement() | ||
|
||
/* get interval and return result array */ | ||
if(getElementPosition == -1) return []; | ||
return getRangeElement = findRangeElement() | ||
|
||
function findElement() { | ||
if (typeof(haystack) === 'undefined' || !haystackLength) return -1; | ||
|
||
var high = haystack.length - 1; | ||
var low = 0; | ||
|
||
while (low <= high) { | ||
mid = parseInt((low + high) / 2); | ||
var element = haystack[mid].substr(0,letterNumber); | ||
element = (case_insensitive) ? element.toLowerCase():element; | ||
|
||
if (element > needle) { | ||
high = mid - 1; | ||
} else if (element < needle) { | ||
low = mid + 1; | ||
} else { | ||
|
||
return mid; | ||
} | ||
} | ||
return -1; | ||
} | ||
function findRangeElement(){ | ||
|
||
for(i=getElementPosition; i>0; i--){ | ||
var element = (case_insensitive) ? haystack[i].substr(0,letterNumber).toLowerCase() : haystack[i].substr(0,letterNumber); | ||
if(element != needle){ | ||
var start = i+1; | ||
break; | ||
}else{ | ||
var start = 0; | ||
} | ||
} | ||
for(i=getElementPosition; i<haystackLength; i++ ){ | ||
var element = (case_insensitive) ? haystack[i].substr(0,letterNumber).toLowerCase() : haystack[i].substr(0,letterNumber); | ||
if(element != needle){ | ||
var end = i; | ||
break; | ||
}else{ | ||
var end = haystackLength -1; | ||
} | ||
} | ||
var result = []; | ||
for(i=start; i<end;i++){ | ||
result.push(haystack[i]) | ||
} | ||
|
||
return result; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
var searchNormal = function(needle, haystack, case_insensitive) { | ||
|
||
|
||
if(needle == "") return []; | ||
var haystackLength = haystack.length; | ||
var letterNumber = needle.length; | ||
var case_insensitive = (typeof(case_insensitive) === 'undefined' || case_insensitive) ? true:false; | ||
var needle = (case_insensitive) ? needle.toLowerCase():needle; | ||
|
||
|
||
for(x=0; x<haystackLength; x++ ){ | ||
var element = haystack[x].substr(0,letterNumber); | ||
element = (case_insensitive) ? element.toLowerCase():element; | ||
|
||
if(element == needle){ | ||
|
||
var getElementPosition = x; | ||
return findRangeElements(); | ||
break; | ||
} | ||
} | ||
|
||
|
||
function findRangeElements(){ | ||
var start = getElementPosition; | ||
for(i=getElementPosition; i<haystackLength; i++ ){ | ||
var element = (case_insensitive) ? haystack[i].substr(0,letterNumber).toLowerCase() : haystack[i].substr(0,letterNumber); | ||
if(element != needle){ | ||
var end = i; | ||
break; | ||
}else{ | ||
var end = haystackLength -1; | ||
} | ||
} | ||
var result = []; | ||
for(i=start; i<end;i++){ | ||
result.push(haystack[i]) | ||
} | ||
|
||
return result; | ||
} | ||
} |