Sorting Algorithms Flashcards

Get familiar with sorting algorithms (17 cards)

1
Q

What is the best and worse case runtime of quick sort

A

O(N log N) and O(n^2)

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

How does quick sort work

A

Pick a pivot and set it in its final place such that all smaller elements are on its left and larger elements are on its right, then repeat on these subsets.

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

What is the runtime of randomised quicksort

A

(Always the same) O(n log n)

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

What is the best and worse case runtime of merge sort

A

O(n log n) for both

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

How does merge sort work

A

Split the list in two then apply merge sort recursively to them, recombine sorted lists back together.

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

What is the runtime of heapSort

A

O(n log n) building heap takes O(n)

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

What is the best and worst case runtime of bubble sort

A

O(n) and O(n^2)

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

How does bubble sort work?

A

Elements are bubbled up, comparing them against the element next to it.

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

How does heapSort work

A

From a heap, swap root node with smallest element and then remove last element. Call max-heapify(a,1) to restore the heap

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

What is a heap

A

A heap is a tree such that the root is the largest element, recursively applies down.

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

What is the best and worse case runtime of insertion sort

A

O(n) and O(n^2)

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

How does insertion sort work

A

Iterate through list, take each element and put it in place in a sorted list

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

What is the best and worse case runtime of selection sort

A

O(n^2) for both

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

How does selection sort work

A

Iterates through the array and puts either the next smallest or largest element in place (start or end of list)

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

How do you prove correctness of an algorithm

A
  1. Create loop invariant
  2. Show that it works for initialisation
  3. Show that it works in maintenance (start/end of each loop)
  4. Show that it works on termination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you calculate the probability of two elements being compared in a quick sort

A

2/(j - i + 1)

Where j is the position in the sorted list of the larger element and i the same for the smaller element

17
Q

What is a recurrence equation

A

An equation for the runtime that depends on the state of n