Sorting Algorithms Flashcards

1
Q

Is quicksort stable?

A

No

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

Can quicksort be done in-place?

A

Yes

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

Can merge sort be done in-place?

A

No. It requires O(n) space for the auxiliary array. There is an in-place version?

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

Is merge sort stable?

A

Yes

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

Is insertion sort stable?

A

Yes

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

Can insertion sort be done in-place?

A

Yes.

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

Can selection sort be done in-place?

A

Yes

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

Is selection sort stable?

A

No

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

Is heap sort stable?

A

No

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

What’s the best-case running time of binary search?

A

O(1) - we get lucky and find the element right at the midpoint.

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

What’s the worst-case running time of binary search?

A

O(log n)

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

How to code selection sort? and time and cost?

A

O(n^2) time

O(1) space – in-place sort

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

How to code insertion sort and cost?

A

Worst case: O(n^2) - everything in reverse/descending order

Best case: O(n) - everything in order/ascending

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

Why is insertion sort quadratic O(n^2) when input is reverse order?

A

each time we insert an element into the sorted portion, we’ll need to swap it with each of the elements already in the sorted list to get it all the way to the start. That’s 1 swap the first time, 2 swaps the second time, 3 swaps the third time, and so on, up to n - 1n−1 swaps for the last item.

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

Pseudo-code for merge sort

A

if len(list) < 2: return list

sorted_first = mergesort(first_half)

sorted_second = mergesort(second_half)

return merge(sorted_first, sorted_second)

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

How to merge 2 sorted lists (during merge sort?)

A

Grab the smallest element from the 2 lists and append to auxiliary array

17
Q

What do you do when merging two sorted arrays and one of the two lists is already completed?

A

Copy over all the elements of the remaining list

18
Q

What is time complexity of merge sort?

A

O(NlogN)

19
Q

What is the base case to break out of merge sort recursion?

A

When the sublist only has 1 element (1 element list is already sorted)

20
Q

Merge sort visualized

A
21
Q
A
22
Q

What can you do to preprocess a collection/list to make searching fasting?

A

Sort (e.g. binary search benefits from sorted list)

23
Q

What is the defintion of an in-place sort?

A

One that uses O(1) space

24
Q

What is the definition of a stable sort?

A

One where entries which ae equal appear in their original order

25
Q

What is the time complexity of python’s sorted and sort method?

A

O(n logn)

26
Q

sorted(students) - in-place/mutate or returns new list?

A

Returns a new list

new_list = sorted(students)

27
Q

students.sort() mutates / in-place or returns new list?

A

In-place / mutates list

28
Q
A
29
Q

How do you pass in a custom key to Python’s sort built-in methods?

A

students.sort(key=lambda x: x.name)

sorted_students = sorted(students, key=lambda x: x.name)

30
Q

When pre-processing a collection/list by sorting first, what is the most likely best time complexity?

A

Depends on the sorting algorithm, but built-ins would be O(n logN)

31
Q

Quick sort time complexity average and best case?

A

O(N logN)

32
Q

Quick sort time complexity worst case?

A

O(n^2) when the input is already sorted

33
Q
A
34
Q

Space complexity of quick sort?

A