Algorithm and data structure time complexity Flashcards

memorize time complexity for algorithms and data structures

1
Q

Insertion sort

A

best: Ω(N)
avg: Θ(N^2)
worst: O(N^2)

Space complexity: O(1)

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

Merge sort

A

best/avg/worst: Θ(N log2 N)

space complexity: O(N)

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

Quick sort

A

best: Ω(N log2 N)
avg: Θ(N log2 N)
worst: O(N^2)

Space complexity: O(log N)

Quicksort worst case: only remove one element from sorted or reverse-sorted data, will only remove one element from data with many duplicates; leads to incremental execution resembling insertion sort: N levels of recursion, N - i elements to partition at level i
Worst case Θ(N^2)

Best case: partitioning is balanced, subproblems no larger than N/2
Θ(N log2 N)

Random pivot selection alters the worst case to become matter of chance for all inputs
possibility of choosing worst pivot in every call gets stalling as N increases
average case Θ(N log2 N) “expected”

Space complexity: O(log N)

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

Heap sort

A

Best/avg/worst: Θ(Nlog2N)

Space complexity: O(1) possible, but I believe method we learned in class was O(N)

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

Array

A

Search/insert/delete: Θ(N)
acces: Θ(1)

Space complexity: O(N)

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

Sorted array

A

Search: O(log N)
Insert/Delete: O(N)

Space complexity: O(N) (?)

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

Linked list

A

Search/access: O(N)
Insert/delete: O(1)

Space complexity: O(N)

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

Stack

A

Search/access: O(N)
Insert/delete: O(1)
(average and worst)

Space complexity: O(N)

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

Hash table

A

Average:
Search/insert/delete: Θ(1)

Worst case:
Search/insert/delete: Θ(N)

if m proportional to N
chaining: get is amortized constant, put is constant ; expected time complexity is Θ(N/m) because of SUHA, worst case O(N)

uniform hashing - expected number of keys compared when inserting an object depends on the load factor N/m
probing: put and get amortized constant under uniform hashing

Space complexity: O(N)

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

Binary search tree

A

Avg search, insert, delete: Θ(log2 N)
Worst search, insert, delete: O(N)

Space complexity: O(N)

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

Red-Black tree

A

Search/access/insert/delete: O(log2 N)
(average and worst)

Space complexity: O(N)

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

BFS

A

O(|V + E|)

space complexity: O(|V|) (worst case hold all nodes in queue)

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

DFS

A

O(|V + E|)

space complexity: height of tree or if using queue get O(|V|)

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

Prim’s

A

O(E log2 V) assuming queue is implemented as a binary heap
queue operations determine the running time
all edges added to queue
worst case: all edges removed from queue
E log2 E = O(E log2 V) as in Kruskal’s

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

Bellman-Ford

A

O(V*E)

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

Dijkstra’s

A

O(E log2 V)

use binary-heap-based priority queue = all operations run in O(log2 V)
one iteration through the graph vertices
each edge relaxed once, aggregate of |E|

17
Q

Priority queue

A

adding and removing O(log2 N) ie limited by height of tree

Space complexity: O(N)

18
Q

Binary search

A

Θ(log2 N)

19
Q

Kruskal’s

A

O(E log2 V)

disjoint set data structure is O(log|V|) for all operations
sorting edges is O(E log2 E)

E < V^2, so log2 E < 2 log2 V and E log2 E = O(E log2 V)
Overall time: O(E log2 V)

20
Q

Queue

A

Search/access: Θ(N)
Insert/delete: Θ(1)

Space complexity: O(N)