Chapter 3: Simple Sorting Flashcards

1
Q

Bubble sort

A

Algorithm: Move left to right doing pairwise compare / swaps. After each iteration, one less item needs to be compared (e.g. last index to check is always decremented)
Complexity: worst-case => n2; average => n2
Invariant: Objects to the right of the outer index are always sorted and are the largest items

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

Selection sort

A

Algorithm: starting from left side of array, does a sequential scan at each index for the next lowest item in the remainder of the array
Complexity: worst-case => n**2 comparisons
Invariant: Objects on the left are always sorted

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

Insertion sort

A

Algorithm: does a single scan through array but at each input must find the objects rightful place and shift all those items greater to the right.
Complexity: worst-case => n**2 comparisons and swaps

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