







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Notes from a university lecture on the correctness proofs of selectionsort and partition algorithms. The lecture covers the definition of a correct algorithm, the concept of loop invariance property, and the proof of correctness for selectionsort. It also discusses the partition algorithm and its loop invariant property.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








George Mason University, Department of Computer Science
R. Paul Wiegand George Mason University, Department of Computer Science
R. Paul Wiegand George Mason University, Department of Computer Science
R. Paul Wiegand George Mason University, Department of Computer Science
R. Paul Wiegand George Mason University, Department of Computer Science
The part to the left of i is a sorted sublist from elements in A The part from i to the right on which the algorithm is still working
Loop Invariant: At the start of each iteration of the outer- most for loop, the sublist A[0... i โ1] con- sists of the i โ 1 smallest elements orig- inally in A in sorted order. The sublist A[i... n โ 1] contain the remaining ele- ments in A.
R. Paul Wiegand George Mason University, Department of Computer Science
Initialization โ At the first iteration, i = 0, the sublist to the left of i is empty. It is reasonable to say that an empty sublist is ordered and obeys the loop invariant. Maintenance โ At the start of any arbitrary iteration, no element from 0 to i โ 1 can ever be disturbed again. During the step, the inner for loop will find the smallest element in A[i + 1... n โ 1] and will swap it for the A[i ] element. This element is swapped into the i th^ position. The new A[i ] element cannot be smaller than any element in A[0... i โ 1] because the loop invariant is true prior to the start of the iteration, so it will simultaneously be the smallest element from i to the right and no smaller than any element to its left. The loop invariant is preserved. Termination โ When the last iteration of the algorithm terminates, the loop counter will be on n โ 2. If the i th^ element is smaller, it will be exchanged with the n โ 1 st^ element. Given that the sublist to the left of i is already sorted and neither A[n โ 2] nor A[n โ 1] can be smaller than any item in A[0... n โ 2] because of the loop invariant, the combined list will be in sorted order. If the i th^ element is not smaller, the list is already sorted.
R. Paul Wiegand George Mason University, Department of Computer Science
if l > r s โโ Partition(A[l... r ]) QuickSort(A[l... s โ 1) QuickSort(A[s + 1... r )
p โโ A[l] i โโ l j โโ r + 1 repeat repeat i + + until p โฅ A[i ] repeat j โ โ until p โค A[j] Swap(A[i ], A[j]) until i โฅ j Swap(A[i ], A[j]) Swap(A[l], A[j])
Loop Invariant (for Partition): At the start of the first repeat loop in the Partition function, the following holds for any array index k: k โ (l, i ] โ A[k] โค p k โ [j, r ] โ A[k] โฅ p
R. Paul Wiegand George Mason University, Department of Computer Science
The part to the left of i (except l) is never greater than the pivot value The part to the right of j is never less than the pivot value The part in the middle on which the algorithm is still working
Loop Invariant (for Partition): At the start of the first repeat loop in the Partition function, the following holds for any array index k: k โ (l, i ] โ A[k] โค p k โ [j, r ] โ A[k] โฅ p
R. Paul Wiegand George Mason University, Department of Computer Science
R. Paul Wiegand George Mason University, Department of Computer Science