Few Definitions
in place: The array A is sorted in place: the numbers are rearranged within the array, with at most a constant number outside the array at any time.
Input size: Depends on the problem being studied.
- Usually, the number of items in the input. Like the size n of the array being sorted.
- But could be something else. If multiplying two integers, could be the total number of bits in the two integers.
Loop invariant
------one damn clever method to show the correctness of algorithms.
Loop invariant: At the start of each iteration of the “outer” for loop the loop indexed by j. The subarray A [1 . . . j −1] consists of the elements originally in A [1 . . . j − 1] but in sorted order.
To use a loop invariant to prove correctness, we must show three things about it:
Initialization: It is true prior to the first iteration of the loop.
Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.
Termination: When the loop terminates, the invariant, usually along with the reason that the loop terminated, gives us a useful property that helps show that the algorithm is correct.
Designing algorithms
Divide and conquer
Divide: the problem into a number of subproblems.
Conquer: the subproblems by solving them recursively.
Base case: If the subproblems are small enough, just solve them by brute force.
Combine: the subproblem solutions to give a solution to the original problem.
Related Algorithm
Insertion sort
Pseudocode
Loop Invariant: elements A[1 ‥ j - 1] are the elements originally in positions 1 through j - 1, but now in sorted order. Correctness of Insertion Sort: · Initialization: when j = 2, the subarray A[1 ‥ j - 1], therefore, consists of just the single element A[1], which is in fact the original element in A[1]. Moreover, this subarray is sorted, which shows that the loop invariant holds prior to the first iteration of the loop. · Maintenance: Informally, the body of the outer for loop works by moving A[ j - 1], A[ j - 2], A[ j - 3], and so on by one position to the right until the proper position for A[ j] is found (lines 4-7), at which point the value of A[j] is inserted (line 8). the second property holds for the outer loop. · Termination: The outer for loop ends when j exceeds n, when j= n + 1. Substituting n + 1 for j in the wording of loop invariant, we have that the subarray A[1 ‥ n] consists of the elements originally in A[1 ‥ n], but in sorted order. But the subarray A[1 ‥ n] is the entire array! Hence, the entire array is sorted, which means that the algorithm is correct. MergeSort Pseudocode
Example:
Loop Invariant: The subarray A[p ‥ k - 1] contains the k - p smallest elements of L[1 ‥ n1 + 1] and R[1 ‥ n2 + 1], in sorted order. Moreover, L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A. Correctness of MergeSort: · Initialization: Prior to the first iteration of the loop, we have k = p, so that the subarray A[p ‥ k - 1] is empty. This empty subarray contains the k - p = 0 smallest elements of L and R, and since i = j = 1, both L[i] and R[j] are the smallest elements of their arrays that have not been copied back into A. · Maintenance: To see that each iteration maintains the loop invariant, let us first suppose that L[i] ≤ R[j]. Then L[i] is the smallest element not yet copied back into A. Because A[p ‥ k - 1] contains the k - p smallest elements, after line 14 copies L[i] into A[k], the subarray A[p ‥ k] will contain the k - p + 1 smallest elements. Incrementing k (in the for loop update) and i (in line 15) reestablishes the loop invariant for the next iteration. If instead L[i] > R[j], then lines 16-17 perform the appropriate action to maintain the loop invariant. · Termination: At termination, k = r + 1. By the loop invariant, the subarray A[p ‥ k - 1], which is A[p ‥ r], contains the k - p = r - p + 1 smallest elements of L[1 ‥ n1 + 1] and R[1 ‥ n2 + 1], in sorted order. The arrays L and R together contain n1 + n2 + 2 = r - p + 3 elements. All but the two largest have been copied back into A, and these two largest elements are the sentinels.