Sorting Flashcards

1
Q

Bubble Sort time and space complexity

A

Time O(n^2), Space O(1)

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

Timsort time and space complexity

A

Time O(n log(n)), Space O(n)

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

what is the property of a heap?

A

heap[k] <= heap[2k+1] and heap[k] <= heap[2k+2]

https://runestone.academy/runestone/books/published/pythonds/Trees/BinaryHeapImplementation.html

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

how does bubble sort work and what’s the complexity?

A

Bubble sort is probably the least efficient sorting algorithm with time O(n^2) and space O(1). Because it can be easily done in place.

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

how does quick sort work and what’s the complexity?

A

quick sort works by splitting the list into two, and then picking a partition point, and then swapping items until the partition condition is met, which is that all values <= to the partition come before the partition, and that all values > partition come after. How you pick the partition has a big impact on the performance in different cases.

Performance
Time O(n log n), Space O(log n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how does merge sort work and what’s the complexity?

A

The basic algorithm is recursive, and breaks the list to be sorted into halves, sorts each half, then merges the result.

Time O(n log n), Space (worst) O(n)

Merging in place could use a deque, or you could use the merge function from heapq.

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