SORTS AND SEARCHES Flashcards
memorise the sorts and searches
1
Q
merge sort?
A
merge sort:
- split the list in half (sub-list)
- keep repeating on each sub-list until the lists only have 1 item
- merge the pairs of sub-lists together sorting the items in the correct order
- repeat step 3 until all sub-lists are merged together
2
Q
bubble sort
A
- look at the first 2 items in the list
- if they are in the wrong order swap them
- move to the next pair (2nd and 3rd item) and repeat
- repeat until you get to the end of the list.
- repeat all steps until there are no swaps left.
3
Q
Insertion sort
A
- the first item is take from the unsorted list and compared to the sorted one. It cant be moved as its the first one.
- the 2nd item is compared to the sorted list, if they’re in the wrong order they’re swapped
- the next is compared and moved into the correct position.
- the next is compared to the sorted list and moved into the right position etc.
https://www.youtube.com/watch?v=jmdP4Y-x0Hc
helpful if still not get (40 secs in)
4
Q
Linear search
A
- look at the first item.
- if this is the right item then stop as you’ve found what you’re looking for.
- look at the next item in the list.
- repeat until the item is found.
5
Q
binary search
A
- sort the list in order.
- find the middle item in the ordered list
- if this is the item then stop
- if not compare the item to the middle one. If it comes before (is less) than the middle item then get rid of the right side of the list. If it comes after (more than) the middle item get rid of the left side of the list.
- repeat until item is found