searching Flashcards
linear search
checks each item of the list systematically to see if it matches the key - O(n)
expected values
the sum of each potential outcome multiplied by its probability:
x1P(X=x1) + x2(P(X=x2) + … + xn*P(X=xn)
sorted linear search
if list sorted in ascending order search can be stopped once an element is reached that is larger than the key
binary search
divide and conquer - calculates mid point of sorted list, if mid point greater than search term discard mid point + if smaller discard mid point -, repeat until key is mid point or no items remain - O(log(n))
SCAN
o(n^2) - for each element all other elements are checked to see if they are equal
STOR
o(n) - loops through once, in a second array the value of the element is used as the key to a second array, that index incremented, if any index > 1 duplicates occur - second array, must have length greater than largest value of first array, and first array must only contain ints
interpolation search
like binary search but uses weighted guess for midpoint - midpoint calculated by [(key - a[i])/(a[j]-a[i]) * j - i]