Link

Algorithm Analysis: Iterative

Complete the Reading Quiz by 3:00pm before lecture.

Table of contents

  1. Selection sort
  2. Asymptotic Analysis
  3. Case analysis

In the previous lecture, we learned about some fundamental principles for analyzing the runtime of a program. In this reading, we will apply the runtime analysis procedure to a sorting algorithm called selection sort. The goal of a sorting algorithm is to rearrange an array of N items into ascending (sorted) order.

Selection sort

Selection Sort
Repeatedly select the smallest remaining item and swap it to its proper index.
  1. Find the smallest item in the array, and swap it with the first item.
  2. Find the next smallest item in the array, and swap it with the next item.
  3. Continue until all items in the array are sorted.

Selection Sort Example

In this example, the array begins unsorted with the items 6 3 7 2 8 1. To find the smallest item in the array, the selection sort algorithm scans across the entire array, and discovers that the value 1 (at index 5) is the smallest. It then swaps the value 1 with the first item in the array. To find the next smallest item in the array, selection sort scans across all but the first item in the array (since we know it’s the smallest), identifies 2 (at index 3), and swaps the value 2 with the second item in the array.

Asymptotic Analysis

Since we are working with a simplified description of selection sort, let’s only consider its comparisons and swaps. Recall from lecture that the term “order of growth” means the simplified relationship between N (the size of the input array) and the cost of the data structure or algorithm. For example, if a particular sorting algorithm performed 2 N2 comparisons and 3 N2 swaps, we would say its order of growth is N2. To determine the order of growth for selection sort, we can either:

  • Make an exact count of its operations and then discard unnecessary pieces.
  • (After lots of practice) use inspection to determine the order of growth.
How many comparisons does it take to find the _smallest_ item in the array in terms of N? How about finding the _next smallest_ item?

It takes N - 1 comparisons to find the smallest item. We can keep track of the smallest item we’ve seen so far in the array and iterate across, comparing each item against the smallest seen so far, updating it if necessary. Initially, the smallest item seen so far is the first item, so we don’t need to compare it against itself.

By the same logic, it takes N - 2 comparisons to find the next smallest item.

The pattern continues until all N items are sorted.

What is the order of growth for number of comparisons in terms of N? First, give an exact count. Then, see if you can come up with a "geometric" argument to describe the same order of growth graphically.

The exact count is N(N - 1) / 2, following the same logic as our analysis for dup1. Another way to think about this is “geometrically”: the green-shaded cells from the example forms a right triangle representing roughly the number of comparisons needed. Then, we know the area of a right triangle with side length N - 1 is roughly N2 / 2.

Using the same steps and reasoning, please analyze the number of swaps on your own. Lastly, let’s try combining these two operations to derive the overall runtime for selection sort.

What is the overall order of growth for selection sort's runtime? Recall that this is based on the number of comparisons and the number of swaps. Give your answer using asymptotic notation.

The number of comparisons has an order of growth of N2 while the number of swaps has an order of growth of just N.

Therefore, the overall order of growth of the runtime for selection sort is in N2.

Case analysis

Selection sort does not have different cases.

Why doesn't the selection sort algorithm have a best or worst case runtime analysis?

There are no shortcuts to finding the minimum item; we always check all of the unsorted items in the array to ensure that we’ve found the smallest item. This is true no matter the contents of the array. Therefore, only N (the size of the array) affects the speed of the algorithm, and there is no best or worst case to consider.


Reading Quiz