Exam Flashcards
What is a peak in an array of numbers?
A peak is a number greater than or equal to its neighbors. The first and last number can be a peak if greater than or equal to its only neighbor.
What is meant by a ‘neighbour’ in the context of peak finding?
A neighbour is a number immediately next to the current number
Is the largest value in an array always a peak?
Yes
What’s the downside of finding the max in a large array?
It looks at every element in the array. This can be inefficient if n is large.
How does the sequential search algorithm find the maximum in an array?
It iterates through the array, comparing each value to the current maximum and updating the maximum when a larger value is found.
In the sequential search, what do “values” and “position” represent?
“values” represents the array (like a shopping list). “position” stores the index of the current maximum.
Is the maximum always the only peak in an array?
No. A peak is a local maximum. There can be multiple peaks.
What does the linear search algorithm look for?
It looks for the first value which is greater than or equal to its neighbors, which defines a peak.
How efficient is the linear search for peaks?
Efficiency depends on the peak’s position.
Best case: 1 comparison. Worst case: n comparisons.
What’s the key concept behind binary search for finding peaks?
It divides the array, focusing on the midpoint, to find a peak by comparing with its neighbors.
What’s the difference between the binary search and binary search with termination?
Binary search with termination stops when ‘start’ is greater than or equal to ‘end’
In an array of length 6, is the first position indexed as 0 or 1?
This depends on the context. In most programming languages, arrays start at index 0, but the given pseudo-code starts at 1.
In a 2D table, how is a peak defined?
A peak is a number greater than or equal to all of its up to 4 neighbours: left, right, above, and below.
How does the Steepest Ascent algorithm work?
It compares an arbitrary element to its neighbours. If smaller, select the largest neighbour and repeat. Otherwise, a peak is found.
What’s the basic idea behind 2D peak finding in the middle column?
Find the largest element in the middle column. Compare with left and right. Throw away half based on comparison till a peak is found.
How efficient is the 2D peak finding algorithm?
Each iteration eliminates half the data. Worst case: Work down to a single column. It takes m*log2n operations.
What are the considerations when determining the “best” algorithm?
Speed, size, generality, and understandability
What is meant by the complexity of an algorithm?
It refers to the rate of growth in the number of operations as the problem size grows.
What is the complexity class of finding the first name in a phone book regardless of its size?
Constant
What is the complexity of finding a phone number in a directory where the numbers aren’t sorted?
Linear
When searching for a specific name in an alphabetically sorted phone book, what is the complexity?
Logarithmic
What is the complexity of the quicksort algorithm?
Linearithmic
In the quicksort example, what value is selected as the pivot initially?
4
What is the complexity of covering extra zeros in n phone books with n entries each?
Quadratic