Algorthims Flashcards
Order of speed of Big O
Fastest to Slowest
Log(n), n, n^2, 2^n, n!
Bubble Sort
Best: n
Average: n^2
Worst: n^2
Stable
1st loop: runs through list i=0 to length-1
2nd loop (inside): runs through list j=0 to length-i-2
Code (inside): If list(j) is greater than list(j+1)
Code(inside): swap numbers
Insertion Sort
Best: n
Average: n^2
Worst: n^2
Stable
2 loops
Runs through list from index = 1 to length -1
Finds value and stores index in new variable
While variable is bigger than 0 and value behind value is greater that value
Swap
End loop
Old variable into new variable position
(Does not Sort first value in list)
Merge Sort
Best: n log n
Average: n log n
Worst: n log n
Divide and conquer
Stable
Quick Sort
Best: n log n
Average: n log n
Worst: n^2 (pivot is bad)
Divide and conquer Fast, good for large data Effective use of caching But complicated Not stable
Dijkstras Shortest Path
Shortest distance between 1 node and all other nodes
A* algorithm
Find shortest path between 1 node and another
Uses heuristics
Linear Search
Search through items one by one until found
O(n)
Unsorted
Binary Search
Divide and conquer
Sorted
O(log n)