Ch 2: Algorithm Analysis & Lists Flashcards
what is linear search?
a search algorithm that starts from the beginning of a list and checks each element until the search key is found or the end of the list is received
- will compare each element if the key is not found
explain linear search algorithm
a single counter starts from the first item and compares it against the search key, if it matches the item is returned otherwise the counter moves to the next item and repeats. if the search key is not found then the counter will have compared each item and “null” is returned
what is binary search?
a faster algorithm for searching a list if the list’s elements are sorted & directly accessible (array)
explain binary search algorithm
first, the middle element of the list is checked, if the search key is found then it is returned. otherwise, if the search key is less than the middle, the middle of the first half of the list is checked for the search key or if the search key is more than the middle, the middle of the second half of the list is checked for the search key. continue to check middle by splitting the list in half
what is the equation to find the middle
0+(N-1) / 2
what are constant time operations?
an operation that for a given processor, always operates in the same amount of time, regardless of input value
what is an example of a constant time operation?
multiplication because the same amount of numbers being multiplied will always have the same time
ex. 12 takes same time as 301000
what is not an example of a constant time operation?
string concatenation because more characters must be copied for larger strings
what affects CTO?
programming language and hardware running the code
what is lower and upper bound?
- lower bound: a function f(N) that is <= the best case T(N) for all values N >= 1
- upper bound: a function f(N) that is >= the worse case T(N) for all values N >= 1
what is an asymptotic notation?
classification of runtime complexity that uses functions that indicate the growth rate of a bounding function
what are the three asymptotic notations that are commonly used in complexity analysis?
O notation: provides a growth rate for an algorithm’s upper bound
Ω notation: provides a growth rate for an algorithm’s lower bound
Θ notation: provides a growth rate that is both an upper and lower bound
what are the rules for Big O notation?
- if f(N) is a sum of several terms, the highest order term ( the one with tte fastest growth rate ) is kept and others are discarded
- if f(N) has a term that is a product of several factors, all constants ( those that are not in terms of N ) are omitted
explain the binary search recursive algorithm
outer function: takes in list
inner function: takes in list (subset), low, high, key
- base case: checks if the low is greater than high, then
- gets mid
- checks if the middle number is less than or greater than the key and makes recursive call with either first half or second half of list
- else returns the middle number
what is a recurrence relation?
a function f(N) that is defined in terms of the same function operating on a value < N
what is a recursion tree?
a visual diagram of an operation done by a recursive function, that separates operations done directly by the function and operations done by recursive calls
explain the different parts of a recursion tree
- root node represents k operations inside the first function call
- recursive operations are represented below the node
- tree’s height corresponds to the number of recursive calls
- an algorithm that performs N operations and then 2 recursive calls will have N/2, continues to divide by 2 as more recursive calls occur