Vous êtes sur la page 1sur 5

CS305 Homework 1

Adam Steinberger September 28, 2011

Correctness of bubblesort

Bubblesort(A) 1 1 for i = 1 to A.length 1 2 for j = A.length down to i + 1 3 if A[j] < A[j 1] 4 exchange A[j] with A[j 1] In order to prove that Bubblesort actually sorts, we need to prove that all original elements in input array A exist in output array A . Because Bubblesort exchanges array elements in A in line 4 (but does not remove any), none of the data from A is lost in the sorting process. Loop Invariant: At the start of each iteration of the for loop in lines 2-4, the subarray A[j + 1 . . . n], where n = A.length, consists of elements originally in A[j + 1 . . . n], but in sorted order. Initialization: A[n] is the original element in A[j], and it is sorted. Maintenance: If A[n 1] < A[n], then according to line 4 we exchange A[n] with A[n 1]. Both original elements of array A still exist, and now they are sorted. Termination: The inner loop in lines 2-4 terminates for every i from line 1. As long as i 1, j will terminate.

Ordering by asymptotic growth rates

The following list of functions is ordered descending from asymptotically largest to smallest (with == inbetween equivalent asymptotic functions). 22
n+1

(1) (2)

2 1

2n

(n + 1)! n! en n 2n 2n n 3 2 (lg n)lg n == nlg lg n (lg n)! == n lg n n3 n == 4


2 lg n

(3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) (16) (17) (18) (19) (20) (21)

(= n

lg 4

= n2 )

n == 2

lg n

lg (n!) (= n = n) ( 2)lg n 2 2 lg n lg2 n


lg 2

ln n lg n ln ln n 1 == n1/ lg n

3
3.1

Parameter-passing costs
Recursive Binary Search

BinarySearchRecursive(A,lef t,right,key) 1 1 midpt = 2 (right lef t) + lef t 2 if key < A[midpt] 3 result = BinarySearchRecursive(A,lef t,midpt,key) 4 else if key > A[midpt] 5 result = BinarySearchRecursive(A,midpt,right,key) 4 else 5 result = midpt 6 return result For the BinarySearchRecursive algorithm for nding a number in a sorted array, we start by comparing a key to the midpoint of the array. If this key is less than the midpoint, we ignore the right half of the array and only search through the left half. The opposite is true if the key is greater than the midpoint. Otherwise we return the midpoint, assuming that the key is now found.

Because every recurrance divides the problem into 1 subproblem of size n/2, we have a worst case running time of T (n) = (lg n). In regards to how the array A is passed to the BinarySearchRecursive function, there are three cases to consider: 1. A is passed by pointer. This operation has a running time of T (n) = (1), so the worst-case running time for the entire algorithm remains T (n) = (lg n). 2. A is passed by copying. This operation has a running time of T (n) = (n). Because the problem is cut in half each time, the worst-case recurrance tree would result in copying the array lg n times. Therefore, the overall running time of the algorithm will be T (n) = (lg n + n lg n) = (n lg n). 3. A is passed by copying only the required subrange of A for each subproblem. This operation has a running time of T (n) = (lg n). Because the problem is cut in half each time, the worst-case recurrance tree would result in copying the array lg n times. Therefore, the overall running time of the algorithm will be T (n) = (lg n + lg2 n) = (lg2 n).

3.2

Merge Sort

Merge-Sort(A,p,r) 1 1 if p < r 2 q = (p + r)/2 3 Merge-Sort(A,p,q) 4 Merge-Sort(A,q + 1,r) 5 Merge(A,p,q,r) Merge(A,p,q,r) 1 1 n1 = q p + 1 2 n2 = r q 3 let L[1 . . . n1 + 1] and R[1 . . . n2 + 1] be new arrays 4 for i = 1 to n1 5 L[i] = A[p + i 1] 6 for j = 1 to n2 7 R[j] = A[q + j] 8 L[n1 + 1] = 9 R[n2 + 1] = 10 i=1 11 j=1 12 for k = p to r 13 if L[i] R[j] 14 A[k] = L[i] 15 i=i+1 16 else A[k] = R[j] 17 j =j+1 3

For the Merge-Sort algorithm for sorting an array, we start by diving the array in half recursively until were left with each element in its own array. Then, we compare each of these array and combine them in sorted order until all elements have been added back to the original array. Because every recurrance divides the problem into 2 subproblems of size n/2, we have a worst case running time of T (n) = (n lg n). In regards to how the array A is passed to the Merge-Sort function, there are three cases to consider: 1. A is passed by pointer. This operation has a running time of T (n) = (1), so the worst-case running time for the entire algorithm remains T (n) = (n lg n). 2. A is passed by copying. This operation has a running time of T (n) = (n). Because the problem is cut in half each time, the worst-case recurrance tree would result in copying the array lg n times. Therefore, the overall running time of the algorithm will be T (n) = (2n lg n) = (n lg n). 3. A is passed by copying only the required subrange of A for each subproblem. This operation has a running time of T (n) = (lg n). Because the problem is cut in half each time, the worst-case recurrance tree would result in copying the array lg n times. Therefore, the overall running time of the algorithm will be T (n) = (n lg n + lg2 n) = (n lg n).

Inductive Proof of Geometric Series


d

Using induction, prove that the geometric series equivalence below is true. xi =
i=0

xd+1 1 x1

(22)

Basis: Assuming that the above equation is true, lets show that this equivalence holds for d = 0.
0

xi = x0 = 1 =
i=0

x0 1 x1

(23) ,

Inductive Step: If the geometric series equation above holds for some k then this must also hold for some k + 1.
k+1 k

xi = xk+1 +
i=0 i=0

xi = xk+1 + xk + xk1 + . . . + x2 + x + 1 x1 x1

(24) (25)

xk+1 + xk + xk1 + . . . + x2 + x + 1

xk+2 + xk+1 + xk + . . . + x2 + x xk+1 xk xk1 . . . x2 x 1 xk+2 1 = x1 x1 (26)

1 The above algorithm pseudo-codes were taken from Introduction to Algorithms 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Cliord Stein.

Vous aimerez peut-être aussi