Algorithms Flashcards
Stacks
- first in, last out
- uses a pointer that points to the element at the top of the list
PEEK - to look at the top item without popping it off
POP - removing the top item of the stack
PUSH - to add an item to the top of the stack
Queues
- first in, first out
- uses two pointers for the front and back. The front holds the first element, and the back stores the next available space
ENQUEUE - adds to the end of the queue
DEQUEUE - removes the first item
Algorithm definition
A series of steps that completes a task
Linked list
A dynamic/ data structure
Each node item consists of data and a pointer
the pointer gives the location of the next node
Trees
Formed from nodes and edges that can be traversed.
Breadth first search
(row by row)
Depth first search
(column by column)
v
Pre-order Traversal
Marker is on the left
root, left leaf, right leaf
In-order traversal
the marker is below the node
left leaf, root, right leaf
Post-order traversal
the marker is on the right
left node, root, right node
Graphs
Set of vertices/ nodes, connected by edges/arcs
Directed graph: edges can be traversed in both directions
Weighted graph: each arc has a cost/ value attached to it.
Binary Search
A O(log(n)) algorithm to search a sorted list for a particular item by repeatedly halving the sublist which contains the item
Bubble sort
A O(n^2) sorting algorithm that iterates through a list comparing each element to its successor and swapping elements if the successor is greater than the current element. This is repeated until no more swaps can be made.
Linear search
A O(n) algorithm to search a list for a particular item by iterating through the list and checking each element until the required item is located, or the end of the list is reached.
Merge sort
A O(n log(n)) divide and conquer sorting algorithm that recursively halves the list into sublists until all sublists are length of 1 item. The sublists are then merged back together and sorted.