Flashcards in Algorithms Deck (43)
binary search precondition
the list needs to be sorted
works by dividing a list into two groups: sorted and unsorted. Elements are inserted one by one into their correct position in the sorted section.
starts at the beginning of a list and checks each item in turn until the desired item is found
divides a list in two each time until the item being searched for is found
Four sorting algorithms
Bubble, Insertion, Quick, Merge
Insertion sort words
Make the first item in the list the sorted list. The remaining items are the unsorted list.
While there are items in the unsorted list take the first item of the unsorted list.
While there is an item to the left of it which is smaller than itself swap with that item. End while.
The sorted list is now one item bigger.
splits a list of size n into n lists of size 1. Each pair of lists are merged together in order until there is only one list of size n.
Why does merge split down into unit lists
because a list of length one is sorted
1. If the sub-list is 1 in length, then that sub-list has been fully sorted
2. If the list is more than 1 in length, then divide the unsorted list into roughly two parts. (An odd numbered length list can't be divided equally in two)
3. Keep dividing the sub-lists until each one is only 1 item in length.
4. Now merge the sub-lists back into a list twice their size, at the same time sorting each items into order
5. Keep merging the sub-lists until the full list is complete once again.
Quick sort algorithm
1. Pick a pivot
2. sort the items either side of the pivot
3. make the items either side of the pivot a sublist
Until length of sublist= 1
The basic idea is to keep splitting a list into two smaller lists, sort those lists using the same quick-sort rules, then split the sub-lists and sort again, until there are only lists of 1 item or less left.
Where can the pivot be applied
The pivot can be applied to any item in the unsorted list.
- long time to run
- Simple to code
- Good performance with small lists
- memory efficient
- good with sequential data
- poor performance with larger lists
- not as quick as merge/quick sort
- difficult to implement
- if a bad pivot is picked the runtime is slow
- worst case efficiency is bad
- very efficient
- no additional storage required/ less stack memory
- Good for sorting slow access data
- Good for sorting sequential data
- if there are two equal values then positions are conserved, which is quicker
- It needs twice then length of memory space than the list
- If recursion is used, it used twice the stack memory as a quick-sort
- quick sort is faster
advantages Linear search
- Good Performance
- List does not need to be ordered
- Not affected by insertions and deletions
disadvantages linear search
- May be too slow over large lists
advantages binary search
- Works well with larger lists
disadvantages binary search
- Small lists simpler to use linear search
- Cannot deal with unordered lists
Shortest path algorithms
Dijkstras and A*
Dijkstras table columns
visited, vertex, shortest distance from start node, previous vertex
What are all the shortest distances filled with at the beginning (Dijkstras)
infinity, except for the vertex your measuring from which is zero
A variation of Dijkstras that uses a heuristic to try and get the correct solution sooner
visited, vertex, shortest distance from start node, heuristic distance, sum, previous vertex