
170 Design and Analysis of Algorithms
7.5 EXAMPLE: SELECTION SORT
In this section we consider a complex example of the development of an algorithm along with its proof of
correctness.
Example 2
Given an array A with index range 1 to N, our task is to permute that is, re-arrange the elements of A,
such that when the algorithm terminates, we have A[1] ≤ A[2] . . . ≤ A[N]. Note that two things are tacitly
assumed: the permutation is in-situ and none of the elements is lost or replaced.
The basic idea behind the algorithm is to view the array A as partitioned into two parts A[1], A[2], . . . ,
A[i − 1] and A[i], . . . , A[N]. The fi rst part will be ...