阿摩線上測驗 登入

申論題資訊

試卷:110年 - 110-2 國立清華大學期末考試題_電機工程學系:計算機程式設計#112945
科目:程式設計
年份:110年
排序:0

題組內容

1. (20%) Answer the following questions briefly.

申論題內容

(b) What is the maximum number of comparisons that a binary search function could possibly make when searching for a value in a 2000-element array? (5%)

詳解 (共 1 筆)

詳解 提供者:hchungw

Binary search is an efficient algorithm for finding an item from a sorted array of items. It works by repeatedly dividing in half the part of the array that could contain the item until you've narrowed down the possible locations to just one.

To find the maximum number of comparisons in a binary search, we use the formula for the height h of a perfect binary tree, which is ℎ=⌈log⁡2(n+1)⌉, where n is the number of elements in the array, and x denotes the ceiling function, which rounds x up to the nearest integer.

For an array of 2000 elements, the maximum number of comparisons would be one more than the height of the tree because you compare once at each level of the tree. Let's calculate this value.

 

The maximum number of comparisons that a binary search algorithm could possibly make when searching for a value in a 2000-element array is 11.