Part 5 Flashcards

1
Q

What is the principle of divide and conquer?

A

A large problem should be broken down into several smaller problems

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

how does divide and conquer work when sorting a collection?

A

split into two collections, sort them, then somehow merge the two collections

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

What are the best, average and worst time complexities of insertion sort?

A

Best: O(n)

Average: O(n2)

Worst: O(n2)

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

What are the best, average, and worst time complexities of mergesort?

A

O(nlogn) for all

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

What are the best, average and worst time complexites of selection sort?

A

O(n2) for all

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

What is the difference between easy-split hard-merge and hard-split easy-merge?

A

ESHM: splitting into two lists is easy, work done in merging sorted sublists

HSEM: splitting is difficult - have to split into large half and small half sublists, but then once they’re sorted can just merge by appending the large list to the small one

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

Is insertion sort easy-split hard-merge, or hard-split easy-merge?

A

Easy split hard merge

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

How does insertion sort work?

A

Split list into two lists of size 1 and n-1

Sort the n-1 list

Insert the 1 into the correct position in the n-1 list

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

Is mergesort easy split hard merge or hard split easy merge?

A

easy split hard merge

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

How does mergesort work?

A

Split list into two equal sized lists

sort them by mergesort

iterate over both lists at the same time

add smaller of the two current items from the two lists to the result array

if one array is empty, append the other to the end of the results list

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

Is selection sort easy split hard merge or hard split easy merge?

A

hard split easy merge

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

how does selection sort work?

A

Swap the pos+1th number of the list with the smallest number from the rest of the list, until the end of the list has been reached

recursive

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

is quicksort easy split hard merge or hard split easy merge?

A

hard split easy merge

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

How does quicksort work?

A

Approximate the median of the list - the pivot. pivot must not be largest number in list so dont count duplicates

split list into two lists where l1 are all <= the pivot and l2 are all > pivot

to find the split point:

using two cursors starting from left and right

left cursor is incremented until an item > the pivot is found

right is decremented until item <= pivot is found

swap item

once cursors have crossed over found the split point

all items left of the cursor are less than the pivot and all on the right are greater

then sort the subarrays using quicksort

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

What is the best, average and worst time complexity of Quicksort?

A

Best: O(nlogn)

Average: O(nlogn)

Worst: O(n2)

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