Part five - Exercise Solutions
Published online by Cambridge University Press: 05 June 2012
Summary
Iterative Algorithms: Measures of Progress and Loop Invariants
Selection Sort: If the input for selection sort is presented as an array of values, then sorting can happen in place. The first k entries of the array store the sorted sublist, while the remaining entries store the set of values that are on the side. Finding the smallest value from A[k + 1] … A[n] simply involves scanning the list for it. Once it is found, moving it to the end of the sorted list involves only swapping it with the value at A[k + 1]. The fact that the value A[k + 1] is moved to an arbitrary place in the right-hand side of the array is not a problem, because these values are considered to be an unsorted set anyway. The running time is computed as follows. We must select n times. Selecting from a sublist of size i takes Θ(i) time. Hence, the total time isΘ(n + (n–1) + … + 2 + 1) = Θ(n2) (see Chapter 26).
- Type
- Chapter
- Information
- How to Think About Algorithms , pp. 411 - 436Publisher: Cambridge University PressPrint publication year: 2008