2.3.1 Flashcards
time complexity of linear search
best average worst
O(1)
O(n)
O(n)
time complexity of binary search
best average worst
O(1)
O(log n)
O(log n)
time complexity of binary tree search
best average worst
O(1)
O(log n)
O(log n)
time complexity of bubble sort
best average worst
O(n)
O(n^2)
O(n^2)
time complexity of insertion sort
best average worst
O(n)
O(n^2)
O(n^2)
time complexity of merge sort
best average worst
O(n log n)
O(n log n)
O(n log n)
time complexity of quick sort
best average worst
O(n log n)
O(n log n)
O(n^2)
linear search
looks at every item in the list
only search that works on unordered lists
binary search
only works on an ordered list of items
gets midpoint of range of items
checks if higher or lower
can discard half of the range
gets midpoint of smaller range
keeps going til item is found
eventually only 1 item will be left
binary tree search
a rooted tree where the nodes are ordered. If order = ascending the nodes of the left subtree have lower values than the root, and to the nodes right have higher values than the root.
to be as efficient as a binary search, the tree must be BALANCED
balanced tree - all leaves are around the same distance from the root.
bubble sort
repeatedly going through a list of items, comparing consecutive pairs of items and swapping the items if they are in the wrong order
pass - a complete set through the list
comparison - 1 pair being compared
maximum passes = n - 1
insertion sort
building up a sorted sublist in the first part of the original list, while the remaining part of the list remains unsorted
card 1, card 2,3,4,5…
then compare card 2 to card 1
card 1,2 card 3,4,5…
then compare card 3 to card 2 if it is less than compare it to card 1
card 1,2,3 card 4,5…
making a sublist that is ordered
merge sort
keep splitting the list until each sublist only contains 1 item
these sublists are merged into sublists of 2
when the items merged they are ordered
this carries on until all items are in 1 list
uses divide and conquer
comapre list 1+2 and 3+4 at the same time
quick sort
you have a: piviot value, low marker, high marker
pick a piviot value (e.g. first value)
all items less than piviot value go before it
all items more than piviot value go after it
to do this the markers are used
low mark is moved up over values that are lower than the pivot value
Once the low mark reaches a value that is higher than the pivot value it stops
high mark is moved down over values that are higher than the pivot value
If the high mark reaches a value that is lower than the pivot value, the algorithm swaps the values of the low mark and the high mark.
The process continues until the high mark overlaps with the low mark, which indicates the new position for the pivot value. Sometimes the pivot value is already in that position.
then 2 new piviots are found in sublists (before and after new piviot place)
process repeats
what type of sort is this pseudocode?
FUNCTION ???_sort(items, start, end)
// Base case for recursion: // The recursion will stop when the partition contains a single item IF start >= end THEN RETURN // Otherwise recursively call the function ELSE pivot_value = items[start] // Set to first item in the partition low_mark = start + 1 // Set to second position in the partition high_mark = end // Set to last position in the partition finished = False // Repeat until low and high values have been swapped as needed WHILE finished == False // Move the left pivot WHILE low_mark <= high_mark AND items[low_mark] <= pivot_value low_mark = low_mark + 1 // Increment low_mark ENDWHILE // Move the right pivot WHILE low_mark <= high_mark and items[high_mark] >= pivot_value high_mark = high_mark - 1 // Decrement high_mark ENDWHILE // Check that the low mark doesn't overlap with the high mark IF low_mark < high_mark THEN // Swap the values at low_mark and high_mark temp = items[low_mark] items[low_mark] = items[high_mark] items[high_mark] = temp // Otherwise end the loop ELSE finished = True ENDIF ENDWHILE // Swap the pivot value and the value at high_mark temp = items[start] items[start] = items[high_mark] items[high_mark] = temp // Recursive call on the left partition ???_sort(items, start, high_mark - 1) // Recursive call on the right partition ???_sort(items, high_mark + 1, end) ENDIF RETURN items ENDFUNCTION
quick sort