0% found this document useful (0 votes)
384 views2 pages

Proof of Correctness - Insertion Sort

Insertion sort works by looping through an array and inserting each element into its sorted position in the preceding part of the array. It maintains the invariant that the portion of the array from index 1 to the current index j-1 is always sorted. To prove correctness, we show that the invariant holds initially for the first element, is maintained as each element is inserted, and results in the full array being sorted when the loop finishes.

Uploaded by

anjali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
384 views2 pages

Proof of Correctness - Insertion Sort

Insertion sort works by looping through an array and inserting each element into its sorted position in the preceding part of the array. It maintains the invariant that the portion of the array from index 1 to the current index j-1 is always sorted. To prove correctness, we show that the invariant holds initially for the first element, is maintained as each element is inserted, and results in the full array being sorted when the loop finishes.

Uploaded by

anjali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Insertion Sort

All sorting algorithms have the property that given a set of keys:

Input: Sequence of numbers <a1,a2,...,an>

Output: Permutation <a1',a2',...,an'> such that a1' ≤ a2' ≤ ... ≤ an'

Algorithm

Insertion Sort accomplishes this task by looping through each element moving it
into place within the preceding elements in the array. Thus insertion sort sorts in
place, i.e. the array can be reordered with a constant (regardless of input size)
amount of extra storage (in this case just a single temp variable for swapping). The
pseudocode for the algorithm is:

INSERTION-SORT(A)
1 for j = 2 to A.length
2 key = A[j]
3 // Insert A[j] into the sorted sequence A[1..j-1]
4 i = j - 1
5 while i > 0 and A[i] > key
6 A[i+1] = A[i]
7 i = i - 1
8 A[i+1] = key
Proof of Correctness

In order to prove the correctness of an algorithm, i.e. that it will work for any input
set, we construct a loop invariant which is a condition that holds for each iteration
of the loop (thus the name invariant). Then the proof consists of verifying that the
algorithm maintains the loop invariant for three conditions:

1. Initialization - the loop invariant holds prior to the first iteration.


2. Maintenance - if the loop invariant is true prior to an iteration, then it
is still true after the iteration.
3. Termination - the loop invariant is true when the loop terminates, i.e.
after the last iteration thus producing the desired result

Note: This procedure is similar to an inductive proof with a base case and
induction step but with added termination criteria.

The loop invariant for insertion sort can be stated as follows:

At each step, A[1..j-1] contains the first j-1 elements in SORTED


order.

The proof of correctness is then straightforward:

Initialization: Prior to the loop j =2 ⇒ A[1.. j-1] = A[1] which contains only
the A[1.. j-1] elements (of which there is only one) and since there is only a single
element they are trivially sorted.

Maintenance: The outer for loop selects element A[ j] and positions it properly
into A[1.. j-1] via the while loop. Since the array A[1.. j-1] began sorted, inserting
element A[j] into the proper place produces A[1.. j] in sorted order (and contains
the first j elements).

Termination: The loop terminates when j= n+1 ⇒ A[1.. j-1] = A[1.. ( n+1)-1]
= A[1.. n] which since the array remains sorted after each iteration gives A[1.. n] is
sorted when the loop terminates (and contains all the original elements) ⇒ the
entire original array is sorted.

You might also like