Algorthims Flashcards

1
Q

Order of speed of Big O

A

Fastest to Slowest

Log(n), n, n^2, 2^n, n!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Bubble Sort

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Insertion Sort

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Merge Sort

A

Best: n log n
Average: n log n
Worst: n log n

Divide and conquer
Stable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Quick Sort

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dijkstras Shortest Path

A

Shortest distance between 1 node and all other nodes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A* algorithm

A

Find shortest path between 1 node and another

Uses heuristics

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Linear Search

A

Search through items one by one until found
O(n)
Unsorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Binary Search

A

Divide and conquer
Sorted
O(log n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly