Sorting and General Flashcards

0
Q

What is the best-case time complexity of insertion sort?

A

O(n)

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

What is the time complexity of insertion sort?

A

O(n^2)

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

What term describes an algorithm which improves in performance for certain inputs?

A

Adaptive

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

What input hits the worst case of insertion sort?

A

Reverse order

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

What input hits the best case of insertion sort?

A

Already sorted

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

What is the space complexity of insertion sort?

A

O(1)

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

How can we improve on trickle-down insertion sort?

A

Instead of swapping all the way down (which uses three assignments per swap), first determine the location to insert at, store a[i] in a temp variable, and trickle up from a[j] (one assignment per iteration)

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

Give the formal definition of what it means for a function f(n) to be O(g(n))

A

There are constants k,N > 0 such that for all n > N . f(n)

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

Give the informal definition of what it means for a function f(n) to be O(g(n))

A

f(n) grows at most like g(n)

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

Give the formal definition of what it means for a function f(n) to be θ(g(n))

A

There are constants j,k,N > 0 such that for all n > N . 0

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

Informally, what does it mean for a function f(n) to be θ(g(n))

A

f(n) grows exactly like g(n)

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

Informally, what does it mean for a function f(n) to be Ω(g(n))?

A

f(n) grows at least like Ω(g(n))

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

What do small-letter complexity classes indicate?

A

A stricter version of the same big-letter classes. E.g. f(n) is o(g(n)) means f(n) grows “strictly more slowly” than g(n)

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

When is amortized analysis useful?

A

When analysing data structure operations which are performed in sequence

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

What technique can be used to minimise the cost of exchanges when sorting arrays of large chunks of data?

A

Sorting by proxy: create an array of indices, do comparisons on the data and exchange the indices

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

What is the maximum number of exchanges needed to sort an array?

A

Θ(n), exactly n-1

17
Q

What is the lower bound on the number of comparisons necessary to sort an array?

A

Ω(n logn)

18
Q

Give a proof of the lower bound on comparisons necessary to sort an array

A

There are n! permutations. Consider any two distinct elements in the array, a and b. If we compare a and b then we halve the number of possible permutations. Hence, we must perform lg(n!) = θ(n logn) comparisons (by Stirling’s approximation)

19
Q

Describe selection sort

A

For each element k in the array, (linearly) find the minimum element in a[k:end] and swap that element with a[k]

20
Q

What is the time complexity of selection sort

A

O(n^2)

21
Q

What is a good algorithm to use when comparisons are expensive but exchanges are very cheap?

A

Binary insertion sort - Binary chop is used to guarantee the minimal O(n logn) comparisons - however exchanges are O(n^2).