Skip to content

Commit

Permalink
#7 22.05.15 > 이분검색 다시 풀기
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed May 15, 2022
1 parent b322845 commit ff18cf9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/inf/sort,greedy/10_re.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

function solution(arr, m) {
let answer = 0;
arr.sort((a,b) => a-b);

let left = 0;
let right = arr.length-1;

while(left <= right) {
let mid = parseInt((left+right)/2);
if(arr[mid] === m) {
answer = mid + 1;
break;
} else if(arr[mid] > m) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return answer;
}

let arr = [23, 87, 65, 12, 57, 32, 99, 81];
console.log(solution(arr, 32));

/*
- 이분검색은 정렬한 배열에서 중간 값을 이용해 주어진 값을 찾는 알고리즘
*/
2 changes: 1 addition & 1 deletion src/inf/sort,greedy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
|7||O|
|8||O|
|9||O|
|10||한번 더 풀기|
|10||O|
|11|X|예정|
|12|X|예정|

0 comments on commit ff18cf9

Please sign in to comment.