Sorting Flashcards

1
Q

Total order

A

A binary relation ≤ that satisfies:
- Antisymmetry: if vw and wv, then v=w.
- Transitivity: if vw and wx, then vx.
- Totality: either vw or wv or both

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

Selection sort

A
  • In iteration i, find index min of smallest remaining entry.
  • Swap a[i] and a[min].
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Insertion sort

A

In iteration i, swap a[i] with each larger entry to its left.

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

Analysis: Selection sort

A

Selection sort uses
(N - 1) + (N - 2) + ... + 1 + 0 ~ N²/2 compares and N exchanges.

Running time is insensitive to input. Quadratic time, even if input is sorted.
Data movement is minimal.

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

Insertion sort: Best & worst case

A

If the array is in ascending order, insertion sort makes N-1 compares and 0 exchanges.

If the array is in descending order (and no duplicates), insertion sort makes ~ ½ N ² compares and ~ ½ N ² exchanges.

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

Knuth shuffle

A

In iteration i, pick integer r between 0 and i uniformly at random.

Swap a[i] and a[r].

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

Convex hull

A

The convex hull of a set of N points is the smallest perimeter fence enclosing the points.

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

Basic plan

Mergesort

A
  • Divide array into two halves
  • Recursively sort each half
  • Merge the two halves
How well did you know this?
1
Not at all
2
3
4
5
Perfectly