Sorting Flashcards

1
Q

What is Quicksorts best, worst and average runtimes? Is it stable and in-place?

A

Best: n log n

Worst: n²

Average: n log n

Stable: Not stable but can be

In-place: Yes

note:

  • stable means to not have the order of 2 equal keys changed i.e. if 5D and 5H were in the initial input array in this order the output array should always hold 5D and 5H one after the other (not 5H, 5D)
  • in-place means to output, for example, an array of the same size as the input and thus not wasting memory.

If the list is implemented as an array, this can be done in-place and with at most n comparisons

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

What is Merge Sorts best, worst and average runtimes? Is it stable and in-place?

A

Best, worst and avg are all

n log n

Stable: Mostly

In-place: No

In any case the number of comparisons is in Θ(n)

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

What is Heap Sorts best, worst and average runtimes? Is it stable and in-place?

A

Best, worst and avg are all

n log n

Stable: No

In-place: Sure?

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

What is BubbleSorts best, worst and average runtimes? Is it stable and in-place?

A

Best: n

Worst: n²

Avg: n²

Stable: Yes

In-place: Yes

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

What is Insertion Sorts best, worst and average runtimes? Is it stable and in-place?

A

Best: n

Worst: n²

Avg: n²

Stable: Yes

In-place: Yes

  • It works well on small lists, or lists that are nearly sorted*
  • number of inversions is n(n − 1)/2 in the worst case ​*(where input is in reverse sorted order)
  • 0 in the best case (input is already sorted)*
  • about n(n − 1)/4 on average case*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Selection Sorts best, worst and average runtimes? Is it stable and in-place?

A

Best, worst and avg are all

Stable: No

In-place: Yes

In practice, it is not as fast as insertion sort

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

Shell Sort

A

Almost nothing is known about average-case running time of Shellsort.

O(n 7/6 ) avg case

Values of h chosen from all relevant numbers of the form 2 i3 j have been proved to give O(n(log n) 2 ) running time

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

Mergesort recurrence I

A

C(n) = 2C(n/2) + n

= 2(2C(n/4) + n/2) + n

= 4C(n/4) + 2n = . . .

= 2kC(1) + kn

= n lg n.

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

Quicksort recurrence

A

Θ(nlogn)

if 1/n instead of 2/n we get

Θ(nl)

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

What are some mergesort properties?

A

Based on a recursive divide-and-conquer approach:

  • If the list has 0 or 1 elements, done.
  • Divide list into two lists of nearly equal size and recursively sort each half.
  • Finally, merge the two sorted halves into one sorted list.

Worst-case running time of Θ(n log n)

One of the best external (disk) sorting algorithms.

Works well for linked lists in addition to arrays/vectors

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

What are some quicksort properties?

A

Quicksort is based on a recursive partitioning about a ‘pivot’:

  • Pick any element as the pivot (many options here).
  • Partition elements into three groups: less than (possibly equal to) pivot, equal to pivot, and more than (possibly equal to) pivot.
  • Run quicksort on first and third partitions until one element.

In practice, a very fast sorting algorithm (almost twice as fast as mergesort)

On average good O(n lg n), but worst case input data leads to Ω( n2 ) performance

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

What are 3 different ways to choose a quicksort pivot?

A
  1. Use passive pivot strategy (e.g. fixed position in range)
  2. Use active pivot strategy (e.g. median-of-three)
  3. Use random pivot strategy (more likely O(n lg n) performance)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is beadsort and what is its runtime complexity range?

A

Sorting algorithm which sorts positive integers by representing them in unary as a sequence of beads on a horizontal lines then uses gravity to sort them with the assistance of vertical rods

The algorithm’s run-time complexity ranges from O(1) to O(S), where S is the sum of the input integers

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

Quicksort recurrence relations and runtime

A

Best case: O(n lg n) is when partitioning gives partitions of the same size. Cost is:

T(n) = 2T((n − 1)/2) + n for unique elements

OR

T(n) = 2T(n/3) + n when duplicates allowed

Worst case: O( n2 ) is when partitioning gives unbalanced partitions.

T(n) = T(n − 1) + n

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