Last active
August 29, 2015 08:09
-
-
Save gribnoysup/9ef419744cebaf3e74ee to your computer and use it in GitHub Desktop.
Binary search, binary search with recursion, rotation count, circular array search
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
// first = true - find first occurance | |
// first = false - find last occurance | |
// first = undefined - first found | |
function binarySearch(array, x, first) { | |
var start = 0, end = array.length - 1, mid = 0, result = -1; | |
while (start <= end) { | |
mid = Math.floor((start + end) / 2); | |
if (array[mid] == x) { | |
result = mid; | |
if (typeof first === 'undefined') { | |
return result; | |
} else if (first === true) { | |
end = mid - 1; | |
} else if (first === false) { | |
start = mid + 1; | |
} | |
} else if (x < array[mid]) { | |
end = mid - 1; | |
} else { | |
start = mid + 1; | |
} | |
} | |
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
// Binary search implemented with recursion | |
function binarySearchRecursion (array, x, low, high) { | |
var _low = typeof low !== 'undefined' ? low : 0; | |
var _high = typeof high !== 'undefined' ? high : array.length - 1; | |
var mid = Math.floor((_high + _low) / 2); | |
if (_low > _high) return -1; | |
if (array[mid] === x) { | |
return mid; | |
} else if (x < array[mid]) { | |
return binarySearchRecursion(array, x, _low, mid - 1); | |
} else if (x > array[mid]) { | |
return binarySearchRecursion(array, x, mid + 1, _high); | |
} | |
} |
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
// Searching for element x in circular (rotated) sorted array witout duplicates | |
// (looking for the sorted parts of the array, then searching for x in these sorted parts) | |
function circularArraySearch (array, x) { | |
var start = 0, end = array.length - 1, mid; | |
while (start <= end) { | |
mid = Math.floor((start + end) / 2); | |
if (array[mid] == x) { | |
return mid; | |
} | |
if (array[mid] <= array[end]) { | |
if (x > array[mid] && x <=array[end]) { | |
start = mid + 1; | |
} else { | |
end = mid - 1; | |
} | |
} | |
if (array[mid] >= array[start]) { | |
if (x >= array[start] && x < array[mid]) { | |
end = mid - 1; | |
} else { | |
start = mid + 1; | |
} | |
} | |
} | |
return -1; | |
} |
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
// Counting rotations in circular (rotated) sorted array | |
// (looking for the pivot point and returning its position) | |
function findRotationCount (array) { | |
var low = 0; | |
var high = array.length - 1; | |
var mid, next, prev; | |
while (low <= high) { | |
if (array[low] <= array[high]) { | |
return low; | |
} | |
mid = Math.floor((low + high) / 2); | |
next = (mid + 1) % array.length; | |
prev = (array.length + mid - 1) % array.length; | |
if (array[mid] <= array[prev] && array[mid] <= array[next]) { | |
return mid; | |
} | |
if (array[mid] <= array[high]) { | |
high = mid - 1; | |
} | |
if (array[mid] >= array[low]){ | |
low = mid + 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment