For Example, the data is:
The key is 6
The key is 6
Record | [1] | [2] | [3] | [4] | [5] |
Data | 2 | 5 | 21 | 1 | 6 |
First step: the data must be sorting. This sample, we can sort the data with bubble sort methods.
Record | [1] | [2] | [3] | [4] | [5] | |
Data | 2 | 5 | 21 | 1 | 6 | |
Step 1 | 2 | 5 | 21 | 1 | 6 | 2 and 5, no Exchange |
2 | 5 | 21 | 1 | 6 | 2 and 21, no Exchange | |
1 | 5 | 21 | 2 | 6 | 2 and 1, Exchange | |
Step 2 | 1 | 5 | 21 | 2 | 6 | 5 and 21, no Exchange |
1 | 2 | 21 | 5 | 6 | 2 and 5, Exchange | |
Step 3 | 1 | 2 | 5 | 21 | 6 | 5 and 21, Exchange |
Step 4 | 1 | 2 | 5 | 6 | 21 | 21 and 6, Exchange |
Second step: we can search data key 6 with binary search
New data:
Record | [1] | [2] | [3] | [4] | [5] |
Data | 1 | 2 | 5 | 6 | 21 |
Step [1]:
(low+high) div 2 = (1+5) div 2
= 6 div 2
= 3---> middle
the data on the record [3] is 5, then 5<6, next step swipe right
low | middle | high | |||
Record | [1] | [2] | [3] | [4] | [5] |
Data | 1 | 2 | 5 | 6 | 21 |
Step [2]: new_low = middle +1 = 3 +1 = 4
new_low | high | ||||
Record | [1] | [2] | [3] | [4] | [5] |
Data | 1 | 2 | 5 | 6 | 21 |
(new_low + high) = (4+5) div 2
= 9 div 2
= 4
the data on the record [4] is 6, then 6=6 , end.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.