[알고리즘] 이진 탐색(Binary Search)

정렬된 리스트에서 특정한 값을 찾는 알고리즘

//이분 탐색 메서드
	private static int binarySearch(int[] A, int n) {
		int first = 0;
		int last = A.length - 1;
		int mid = 0;

		while (first <= last) {
			mid = (first + last) / 2;

			if (n == A[mid])
				return 1;
			else {
				if (n < A[mid])
					last = mid - 1;
				else
					first = mid + 1;
			}
		}
		return 0;
	}

문제

백준 1920

정리된 코드

B_1920