Big O Notation Flashcards

(23 cards)

1
Q

What is the order from the best O time complexities to the worst ones?

A

O(1), O(log n), O(sqrt n), O(n), O(n log n), O(n^2), O(2^n), O(n!)

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

How does O(1) work?

A
  • Same time returned regardless of input size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does O(n) work?

A
  • Uses entire input size once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does O(log n) work?

A
  • Runtime depends on half the input size
  • Continues to somewhat halve
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does O(n^2) work?

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

How do you determine the O value of two independent methods?

A

O(a + b)

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

How do you determine the O value of nested loops?

A

O(a * b)

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

What are the time complexities of binary search?

A

Best: O(1)
Other: O(log n)

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

What are the space complexities for all the sorting/searching methods?

A
  • O(1): both bubbles, selection, insertion,
  • O(log n): quick sort
  • O(n): merge sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the time complexities of bubble sort?

A

O(n^2) for all

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

What are the time complexities of optimized bubble sort?

A

Best: O(n)
Others: O(n^2)

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

What are the time complexities for selection sort?

A

O(n^2) for all

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

What are the time complexities for insertion sort?

A

Best: O(n)
Others: O(n^2)

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

What time complexity does String.substring() give?

A

O(n)

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

What time complexity does String.indexOf() give?

A

O(n)

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

What time complexity does ArrayList.remove(i) give?

17
Q

What time complexity does String.add(i, val) give?

18
Q

What time complexity does String.get(i) give?

19
Q

What time complexity does a nested loop that shrinks give? What can shrink?

A

O(n^2)
- y can have j < i or j < n

20
Q

What does binary recursion give off?

21
Q

When you include the use of strings in a loop, what is the time complexity given?

22
Q

How do insertion and selection sort compare on an array that is mostly sorted?

A
  • insertion sort goes through less comparisons and shifts overall
  • selection sort compares every index every pass
23
Q

How do insertion sort, bubble sort, and selection sort compare in space usage?

A

All are the same