Quadratic Sorts

Bubble sort

Algorithm.

for i = n downto 2 do
	for j = 1 to i-1 do
		if A[j] > A[j+1] then
			A[j] <-> A[j+1]

Complexity (comparisons). The conditional is executed on every iteration of the inner loop. Therefore, the number of comparisons is the same for all inputs of size n.

Complexity (exchanges). The best case occurs when the list is already sorted. Here, the conditional is never executed and 0 swaps occur.

The wost case occurs when the list is reversed. Here, the conditional is always true and therefore swaps and comparisons are equivalent: (n1)n2.

The average case is more difficult to understand. In a randomly distributed array, the probability that any pair of elements will be transposed is 12. Since every pair of transposed elements requires a swap we take half the total pairs. This is 12n2=(n1)n4.

Insertion sort (with sentinel)

Algorithm.

A[0] = -inf
for i = 2 to n do
	t = A[i]
	j = i-1
	while t < A[j] do
		A[j+1] = A[j]
		j = j-1
	A[j+1] = t

Complexity (comparisons). All comparisons occur in the while loop.

For the best case, the while loop never actually executes, requiring n1 comparisons.

For the worst case, the while loop executes until reaching the sentinel every time (reverse sorted).

The average case requires calculating the expected value. The outer summation with index i represents the for loop and the summation with index j calculates the expected value of comparisons for all possible elements A[i]. Since A[i] is equally likely to land anywhere in the subarray A[1i], take the expected value over all those possible positions.

Complexity (exchanges). For the purposes of this analysis we will also count the sentinel assignment.

For the best case, you only move to the temporary variable t and then move back to the array. This requires 2n1 exchanges.

For the worst case, you must move every sorted element. Instead of calculating a summation we can see there’s a relationship between comparisons in the worst case and moves. Inside the for loop we do one more move than comparison.

For the average case, we use apply the same relationship technique.

Insertion sort (without sentinel)

Algorithm.

for i = 2 to n do
	t = A[i]
	j = i-1
	while j > 0 and t < A[j] do
		A[j+1] = A[j]
		j = j-1
	A[j+1] = t

Complexity (comparisons). All comparisons occur in the while loop. We don’t count the boundary check as an extra comparison even if it would take time in real life.

For the best case, the while loop never actually executes, requiring n1 comparisons.

For the worst case, the while loop executes until reaching the edge of the array every time (reverse sorted).

The average case is equiavalent to the non-sentinel version except we do no sentinel comparisons. Therefore, if we can calculate the average number of sentinel comparisons, we have our answer.

Therefore, the total comparisons is (n1)(n+4)4Hn+1.

Complexity (exchanges). This is not affected by the sentinel at all.

Selection sort

Algorithm.

for i = n downto 2 do
	k = 1
        	for j = 2 to i do
		if A[j] > A[k] then
			k = j
	A[k] <-> A[i]

Complexity (comparisons). The analysis is equivalent for all possible cases. There is a comparison every inner iteration no matter what.

Complexity (exchanges). Once again, the anlysis holds for all possible cases. There is an exchange every outer iteration.