일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- sqs fifo queue
- 티스토리챌린지
- goland
- 구조체
- GIT
- context7
- golang
- model context protocol
- elasticsearch
- cosine similarity metric
- redirect-gateway
- replication lag
- ssh 에이전트
- authorizationpolicy
- 2024 톨스토이문학상 수상
- 서비스메쉬
- 캡슐화
- AWS
- 디자인패턴
- Infra
- 오블완
- typescript
- esbuild
- Intellij
- javascript
- AI
- go
- GoF
- Kubernetes
- RDS
Archives
- Today
- Total
Fall in IT.
이진탐색 알고리즘(Binary Search Algorithm) 본문
반응형
안녕하세요.
오늘은 이진탐색 알고리즘에 대해서 알아보도록 하겠습니다.
이진탐색 알고리즘(Binary Search Algorithm)
- 1회 비교를 거칠때마다 탐색 범위가 대략 절반으로 줄어들기 때문에 이진탐색이라고 불린다.
- 배열의 값들이 정렬되어 있을때만 사용이 가능합니다.
function solution(element, some_list) {
let firstIndex = 0;
let lastIndex = some_list.length - 1;
while(firstIndex <= lastIndex) {
let centerIndex = Math.floor((firstIndex + lastIndex) / 2);
if (element === some_list[centerIndex]) {
return centerIndex;
}
else if (element < some_list[centerIndex]) {
lastIndex = centerIndex - 1;
}
else {
firstIndex = centerIndex + 1;
}
}
return "None";
}
console.log('solution: ', solution(2, [2, 3, 5, 7, 11])); // solution: 0
모두 즐거운 코딩 하세요~
반응형
'Algorithm' 카테고리의 다른 글
프로그래머스 코딩테스트 [해시] - Javascript (1) | 2019.10.28 |
---|---|
프로그래머스 코딩테스트 [해시] - Javascript (0) | 2019.10.28 |
프로그래머스 코딩테스트 [스택/큐] - Javascript (0) | 2019.10.25 |
프로그래머스 코딩테스트 [깊이/너비우선탐색 (DFS, BFS)] - Javascript (1) | 2019.10.24 |
프로그래머스 코딩테스트 [완전탐색] - Javascript (1) | 2019.10.21 |
Comments