7-9 Flashcards

1
Q

What is a simple way to improve mergesort?

A

Make it drop down to insertion sort when approximately 40 items are left in the array.

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

How does quicksort work?

A

take the first value (or middle value out of first, middle and last, or random value in the array). We then use this value to seperate the array into two sub arrays, the left has all smaller values and the right has all larger values. This could be done recursively until the array length is 1 or less and then combined.

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

Analyse quicksort.

A

Has big theta of n log n, this occurs when there are log n splits of the array(always perfect split). In the worst case it has a time complexity of n^2, which is a completely unbalanced split. Any kind of proportional split e.g average 90% is still good.

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

How can we improve quicksort?

A

Pick a small size and let the quicksort subarrays be returned without sorting further. Use insertion sort to finish as insertion sort is quick on nearly sorted data.

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

What is a stable sorting algorithm?

A

One in which equal keys are left in the same relative order.

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

How do we sort in linear time?

A

Use a non comparison sort like counting, bucket and radix sort.

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

What is counting sort?

A

A linear time non comparison sort which requires the integers to be in a small range, number of data items roughly the same as size of largest key.
It works by working out for each input key how many keys are smaller, it can then place the key in the that number+1 spot.
It is stable.

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

What is bucket sort?

A

A non comparison sort which has three phases:

  1. Distribute keys into buckets (based on characteristics like first letter or comparison to pre-determined values). O(n)
  2. Sort the buckets(keys must be uniformly distributed), using a comparison sort function.
  3. join the buckets. O(n).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is radix sort?

A

Sort by creating buckets using the rightmost digit only first and sorting that, then recombine all buckets and repeat for second to rightmost digit etc. The digit sort must be stable to work. If the keys don’t all have the same number of digits add leading 0s

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