티스토리 뷰
반응형
👨💻 특징
- binary search (이진 탐색)
- 주어진 리스트를 절반씩 줄여가며 탐색
- O(logN)의 속도
- 이미 정렬되어 있는 리스트에서만 사용 가능
🚦 필요한 경우
- 선형 순차 탐색보다 빠른 탐색이 필요하고 리스트가 정렬되어 있는 경우 사용한다.
💾 코드
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 32 33 34 35 36 | # iteration 사용 def b_search_e(lst, target): start_idx = 0 end_idx = len(lst)-1 # idx의 역전이 일어날 때까지 순회 while start_idx <= end_idx : # 기준값 설정 target_idx = (start_idx + end_idx)//2 if lst[target_idx] > target: end_idx = target_idx - 1 elif lst[target_idx] < target: start_idx = target_idx + 1 else : return target_idx return -1 # recursive 사용 def b_search_r(lst, target, start_idx, end_idx): # 기저조건 생성 if start_idx > end_idx : return -1 # 기준 index 생성 target_idx = (start_idx + end_idx) // 2 # 재귀를 할 때 return을 해주어야 한다. return으로 결과값을 도출하지 않으면 그냥 선언일 뿐이다. if lst[target_idx] > target: return b_search_r(lst, target,start_idx, target_idx - 1) elif lst[target_idx] < target: return b_search_r(lst, target, target_idx+1, end_idx) else: return target_idx | cs |
반응형
'알고리즘 학습 > 알고리즘 개념' 카테고리의 다른 글
연결 리스트(Linked List) (0) | 2020.09.15 |
---|---|
정규표현식 (0) | 2020.09.09 |
최적 부분 구조(Optimal Substructure) (0) | 2020.09.04 |
Quick Sort (퀵소트) (0) | 2020.08.19 |
Linear 한 리스트에서 두 수의 합 찾기 (0) | 2020.07.26 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 날짜 계산#BOJ#완전탐색#Python
- 백준 알고리즘#BackTracking
- 텀 프로젝트#백준알고리즘#Python
- django
- 쿼드트리#BOJ#분할정복#Python
- 랜선자르기#이분탐색#BOJ#Python
- API#lazy#
- Distinct#Codility#Python
- filter#isalnum#lower
- 순열사이클#BOJ#Python
- 나무자르기#BOJ#이분탐색#Python
- 공유기 설치#BOJ#이분탐색#Python
- django#slicing
- PassingCars#Codility#Python
- 종이자르기#분할정복#BOJ#Python
- N으로 표현#DP#Programmers#Python
- Triangle#Sorting#Codility#Python
- 반복수열#백준알고리즘#Python
- 미로 탐색#백준알고리즘#Python
- 암호코드#dp#BOJ#Python
- Swift#Tuples#Range
- 리모컨#완전탐색#BOJ#Python
- 토마토#백준알고리즘#Python
- 파이썬알고리즘인터뷰#4장
- 터틀비치#리콘#xbox#controller
- 배열합치기#분할정복#BOJ#Python
- 섬의개수#백준알고리즘#Python
- Brackets#Stacks and Queues#Codility#Python
- NumberofDiscIntersections#Codility#Sort#Python
- 병든 나이트#BOJ#탐욕법#Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함