2.3 Algorithms Flashcards
What are the maximum number of passes in a bubble sort?
n^2
What are the maximum number of passes in an insertion sort?
n^2
What are the maximum number of passes in a linear search?
n
What are the maximum number of passes in a binary search?
log(n)
What is the method of merge sorts?
List is split in half every time until there is only 1 item in a sub list
Individual sub lists are then merged together into order until list is complete again
What is a partition in a quick sort algorithm?
An unsorted section of the list
To sort items, list is repeatedly partitioned
Divide and conquer approach means when each partition only contains one items, list is sorted
What is a pivot value?
Value chosen to partition the list which is being sorted
New value chosen each time list is partitioned
What are the markers of a quick sort?
To keep track of items as they are in order, uses two marks
Lowmark and highmark
What is the method of a quick sort?
- choose value as pivot (usually 1st item in list)
- take two variables to point left and right of list excluding pivot
- left points to low index and right points to high
- while value at left is less than pivot, move right
- while value at right is greater than pivot, move left
- if both step 4 and 5 don’t match, swap left and right values
- if left greater or equal to right, point where they met is new pivot
What is Dijkstra’s algorithm?
Finds the shortest path between a start node and every other node in a weighted graph
What are the uses of Dijkstra’s algorithm?
Scheduling aeroplanes and staff so air crews always have suitable rest time
Finding best move in chess fame
Timetabling classes in a school
Finding shortest path between two points (circuit boards, route planning GPS, communication networks)
What could the weights of a graph represent?
Distances
Time
Cost of travel
How is Dijkstra’s algorithm implemented?
Similar to breadth-first search
Could use different data structures (queue, array)
Starts by assigning temporary distance value to each node
Temporary distance is 0 at start node and ∞ at every other node
How is the distance to a node calculated?
Distanced from connected node + distance from start to connected node
How is the next current node selected?
Unvisited node with shortest distance from the start
What are the column names for the table in Dijkstra’s algorithm?
Node
Visited
Shortest distance from start
Previous node
What is a heuristic approach?
One which tries to find a solution which may not be perfect but which is adequate for its purpose
What is A* algorithm?
Makes use of heuristics as each node has a heuristic value associated to it
Makes an ESTIMATE of the distance from each node to the goal node, which may not be perfect but can be used to determine most probable route and therefore don’t need to check all nodes
What can a GPS app use as a heuristic value?
Expected distance from end
Level of congestion
Speed restrictions
What are the functions of an A* algorithm?
g(x) = distance from start (weight of node + distance to node)
h(x) = the approximate cost from node(x) to goal node