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
Which complexity class grows as 2^n?
Exponential
What is meant by an algorithm having factorial complexity?
The number of iterations grows as n!, where n! = 1x2x3x…xn.
When comparing algorithms, what do we usually use to refer to the problem size?
n.
What’s an informal way to think about problem size?
How many things the problem contains, like the number of items to be sorted.
What is the main property that a number must have to be considered a peak?
It must be greater than or equal to its neighbors.
How do we informally compare the speed of algorithms?
By looking at how many operations they perform. More operations typically mean longer execution time.
What is the time complexity of a linear search for peak finding?
O(n).
Name a common method to compare algorithms
Big O notation
What does O(1) represent
Constant time complexity
Define P in complexity classes
It represents the class of problems that can be solved in polynomial time
What is a 2D peak in a matrix
An element greater than or equal to its adjacent elements both horizontally and vertically.
How does binary search help in peak finding
It reduces the problem size by half in each iteration
What does NP stand for in complexity classes
Nondeterministic Polynomial time
Which class represents problems for which answers can be verified in polynomial time?
NP
What does O(n^2) signify
Quadratic time complexity
What is an array in data structures
An array is a data structure consisting of a fixed number of data items of the same type.
How is any array element accessed?
Any array element is directly accessible via an index value.
Can arrays have more than one index?
Yes, those are called multidimensional arrays.
What special pointers are maintained in lists?
Head (and tail for doubly linked lists) to point to the first (and last) elements
How many operations does initializing an array of n elements take?
n operations
Describe a stack.
A stack holds multiple elements of a single type and follows the LIFO principle - Last In First Out.
What is the procedure to add an element to a stack?
‘push’
How can a stack be implemented?
With an array and an integer counter to indicate the current number of elements in the stack.