Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이진 탐색(Binary Search) 알고리즘 정리 #5

Open
dididy opened this issue Mar 9, 2021 · 1 comment
Open

이진 탐색(Binary Search) 알고리즘 정리 #5

dididy opened this issue Mar 9, 2021 · 1 comment
Assignees

Comments

@dididy
Copy link
Member

dididy commented Mar 9, 2021

개념

정렬된 데이터가 있을 때 특정 값을 효율적으로 찾을 수 있는 알고리즘이다. 가운데 값을 기준으로 오른쪽, 왼쪽 각각의 값보다 큰지 작은지를 파악하여 해당 범위의 중앙값을 선택하여 반복하는 과정을 수행하면 원하는 값을 찾을 수 있다.

구현

function binarySearch(target, array) {
  let start = 0;
  let end = array.length - 1;
  while (start <= end) {
    let mid = Math.floor((end + start) / 2);
    const guess = array[mid];

    if (guess === target) {
      return guess;
    }

    if (guess > target) {
      end = mid - 1;
      continue;
    }

    start = mid + 1;
  }

  return undefined;
}

시간복잡도

O(logN)

@dididy dididy self-assigned this Mar 9, 2021
@dididy
Copy link
Member Author

dididy commented Mar 9, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant