Search algorithms Flashcards
linear search algorithm
traverse the list using a for loop
compare each item to the search term
if there is no match it is not in the list
big - O complexity of the linear search
O(n)
num operations directly proportional to num items in the list
returned value of linear search function
boolean - True or False
linear search benefits and limitations
linear search can be used on any list, but takes more operations than binary search
binary search
find midpoint
compare to search term
discard half of the list where the search term is not found
repeat until found or list is down to one item
Big- O complexity of binary search
O(log n)
two ways of implementing binary search
- slicing list into smaller list
- using “start” and “stop” markers to mark the ara of the list where the search term might occur
benefits and limitations of binary search
- faster than linear search
- can only be used with an ordered list
five levels of big O complexity in order
constant - O(1)
logarithmic - O(log n)
linear - O(n)
polynomial- O(n^x)
exponential - O(x^n)
how to define the complexity of an algorithm
constant - no loop
logarithmic- dividing a list in half repeatedly
linear - a for loop
polynomial - a loop inside a loop
exponential - an even more complex program
how to determine the complexity of an expression
Ignore the actual numbers
Look for any places where n occurs in the expression
Compare this to the big-O levels to find a match to the shape
If there is more than one example, take the most complex
two ways to swap values
tuple substitution
three- line swap using a “temp” variable
bubble sort
traverse list
compare each value to the next, swap if wrong way around
traverse multiple times until there are no swaps
how to recognise bubble sort
a for loop inside a while loop
big- O complexity of bubble sort
polynomial
O(n^x)