Chapter 11, Computer Science Thinking: Recursion, Searching, Sorting and Big O 7
quadratic run time and pronounced “on the order of n–squared” or more simply
“order n–squared.”
b. Big O is concerned with how an algorithm’s run time grows in relation to the
number of items, n, processed. When n is small, O(n2) algorithms (on today’s su-
per-fast computers) will not noticeably affect performance, but as n grows, you’ll
start to notice performance degradation.
c. An O(n2) algorithm operating on a billion-element array (not unusual in today’s
big-data applications) would require a quintillion operations, which on today’s
desktop computers all the other array elements could take approximately 13.3
years to complete! O(n2) algorithms, unfortunately, are easy to write.
d. All of the above statements are true.
11.8 Q4: Which of the following statements is false?
a. The linear search algorithm runs in O(n) time.
b. The worst case in the linear search algorithm is that every element must be
checked to determine whether the search item exists in the array. If the size of
the array is doubled, the number of comparisons that the algorithm must perform
is quadrupled.
c. Linear search can provide outstanding performance if the element matching
the search key happens to be at or near the front of the array.
d. Linear search is easy to program, but it can be slow compared to other search
algorithms. If a program needs to perform many searches on large arrays, it’s bet-
ter to implement a more efficient algorithm, such as the binary search.
11.9 Binary Search
11.9 Q1: Which of the following statements about binary search of an array in
ascending order is false?
a. The binary search algorithm is more efficient than linear search, but the linear
search requires a sorted array.
b. The first iteration of this algorithm tests the middle element in the array. If this
matches the search key, the algorithm ends.
c. If the search key is less than the middle element, it cannot match any element
in the second half of the array so the algorithm continues with only the first half
of the array (i.e., the first element up to, but not including, the middle element).
d. If the search key is greater than the middle element, it cannot match any ele-
ment in the first half of the array so the algorithm continues with only the second
half of the array (i.e., the element after the middle element through the last ele-
ment).