Home > Back-end >  Consult the teacher - binary search questions - soft junior programmer
Consult the teacher - binary search questions - soft junior programmer

Time:12-11

The key sequence (9,12,15,20,24,29,56,69,87) binary search (binary search), if you're looking for key 15; You have, in turn, compared with (38),
87,29,15 (38) a. b. 9,12,15 c. 24,12,15 d. 24,20,15

Excuse me:
1. Among the first number is 24, is after 15 out of 29 56, 69, 87, and the rest of the series
9 to 12 in the 15 to 20 which is the middle number
2. Why the answer is not C D

CodePudding user response:

Used deduction
First median 24 after comparison, the rest of the series is 9 15 20, 12 comparison obviously only c is in accordance with,

CodePudding user response:

The middle number is the array length len/2, is divided into three areas, the entire array [0 ~ len/2), len/2, (len/2 ~ n - 1) and then in turn determine
If the key==a len/2, return success
If keyIf key> A len/2, for processing (len/2, n - 1]
The second median, 4/2 is 2 is [9,12,15), 15 (15, 20]
  • Related