귀하는 로그인되어 있지 않습니다. 이대로 편집하면 귀하의 IP 주소가 편집 기록에 남게 됩니다.스팸 방지 검사입니다. 이것을 입력하지 마세요!=== 이분검색 === 문제 : 10개의 정렬된 배열에서 값을 21가진 인덱스를 찾아라. low: 0 high: 9 middle: (low + high)/2 => 4.5 => 4 {| class="wikitable" style="text-align: center;" | style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(0)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(1)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(2)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(3)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(4)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(5)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(6)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(7)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(8)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(9)''' |- | style="background-color: #CAF4FA; color: black; font-size: bold;" | 5 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 7 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 10 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 12 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 16 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 20 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 21 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 25 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 28 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 30 |}<br> low: 4 + 1 => 5 high: 9 middle: 7 {| class="wikitable" style="text-align: center;" | style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(0)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(1)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(2)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(3)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(4)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(5)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(6)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(7)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(8)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(9)''' |- | style="background-color: #FF8800; color: black; font-size: bold;" | 5 || style="background-color: #FF8800; color: black; font-size: bold;" | 7 || style="background-color: #FF8800; color: black; font-size: bold;" | 10 || style="background-color: #FF8800; color: black; font-size: bold;" | 12 || style="background-color: #FF8800; color: black; font-size: bold;" | 16 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 20 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 21 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 25 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 28 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 30 |}<br> low: 5 high: 7 - 1 = 6 middle: 5 {| class="wikitable" style="text-align: center;" | style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(0)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(1)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(2)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(3)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(4)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(5)''' || style="background-color: #00BCD4; color: white; font-size: bold;" | '''A(6)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(7)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(8)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(9)''' |- | style="background-color: #FF8800; color: black; font-size: bold;" | 5 || style="background-color: #FF8800; color: black; font-size: bold;" | 7 || style="background-color: #FF8800; color: black; font-size: bold;" | 10 || style="background-color: #FF8800; color: black; font-size: bold;" | 12 || style="background-color: #FF8800; color: black; font-size: bold;" | 16 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 20 || style="background-color: #CAF4FA; color: black; font-size: bold;" | 21 || style="background-color: #FF8800; color: black; font-size: bold;" | 25 || style="background-color: #FF8800; color: black; font-size: bold;" | 28 || style="background-color: #FF8800; color: black; font-size: bold;" | 30 |}<br> low: 5 + 1 = 6 high: 6 {| class="wikitable" style="text-align: center;" | style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(0)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(1)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(2)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(3)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(4)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(5)''' || style="background-color: #1FFF3D; color: white; font-size: bold;" | '''A(6)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(7)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(8)''' || style="background-color: #DB1A1A; color: white; font-size: bold;" | '''A(9)''' |- | style="background-color: #FF8800; color: black; font-size: bold;" | 5 || style="background-color: #FF8800; color: black; font-size: bold;" | 7 || style="background-color: #FF8800; color: black; font-size: bold;" | 10 || style="background-color: #FF8800; color: black; font-size: bold;" | 12 || style="background-color: #FF8800; color: black; font-size: bold;" | 16 || style="background-color: #FF8800; color: black; font-size: bold;" | 20 || style="background-color: #02F758; color: black; font-size: bold;" | 21 || style="background-color: #FF8800; color: black; font-size: bold;" | 25 || style="background-color: #FF8800; color: black; font-size: bold;" | 28 || style="background-color: #FF8800; color: black; font-size: bold;" | 30 |}<br> <syntaxhighlight lang='java'> int[] arr = {5, 7, 10, 12, 16, 20, 21, 25, 28, 30} public int binarySearch(int target) { low = 0; high = arr.length; while (low <= high) { middle = (low + high) / 2; if (target == array[middle]) { return middle; } else if (target > array[middle]) { low = middle + 1; } else { high = middle - 1; } } return -1; //not found } </syntaxhighlight> 편집 요약 가온 위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 가온 위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요. 또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요! 취소 편집 도움말 (새 창에서 열림)