Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 이진 탐색 ( binary search )
- * @param {array} list - Sorted Array.
- * @param {*} item - The key for search.
- * @returns {number} idx - Index of key in array. -1 means "can't find the key."
- */
- function binary_search(list, item) {
- // init
- var idx = -1;
- var low = 0;
- var high = list.length - 1;
- var mid = 0;
- var guess = 0;
- while (low <= high) {
- mid = Math.ceil((low + high) / 2);
- guess = list[mid];
- if (guess == item) {
- return mid;
- } else if (guess > item) {
- high = mid - 1;
- } else {
- low = mid + 1;
- }
- }
- return -1; // 못찾음
- }
Add Comment
Please, Sign In to add comment