selection sort algorithm
selectionSort(A,n)
min = 0
for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1 to n-1)
if (A[j] < A[min])
min = j;
swap(A[min], A[i]);
}
Proof using Loop Invariant
Loop Invariant : After i iteration ,all elements from index 0 to
i-1 array A[] is sorted in non decreasing order.And all entries in
A[i..n-1] are larger than or equal to the entries in A[0..i-1]
Base case :initially (when i=0),there is no any element from index 0 to -1.
Maintenance: In each outer loop iteration,inner loop finds minimum element in A[i to n-1] and put it at ith place.which make array A[0 to i] sorted in non decreasing order(Because all entries in A[i..n-1] are larger than or equal to the entries in A[0..i-1]).
Termination: when i=n all element in array A[0..n-1] are sorted in non decreasing order.
Get Answers For Free
Most questions answered within 1 hours.