


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
Material Type: Notes; Class: LINEAR ALGEBRA; Subject: Mathematics; University: University of California - Irvine; Term: Spring 2003;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Jim Lambers Math 6A Spring Quarter 2003- Lecture 16 Examples
These examples correspond to Section 5.3 in the text.
Example Suppose that we need to sort n real numbers x 1 , x 2 ,... , xn in ascending order. We are using the algorithm known as insertion sort. This algorithm copies the numbers in an array A, of size n, to another array B, also of size n, one at a time in such a way that B is always sorted. It maintains the sorted order in B by shifting elements as needed when a new one is added. The following pseudo-code describes how insertion sort can be implemented.
for i = 1 to n do j = i − 1 while j ≥ 1 and B[j] > A[i] do B[j + 1] = B[j] (shift element to make room for new element) j = j − 1 end B[j] = A[i] (place new element in correct position) end
One way to measure the complexity of this algorithm is in terms of the number of times elements need to be shifted in order to maintain the sorted order of B. In the best case, when the array A is already sorted, no shifting is required. In the worst case, when A is sorted in descending order, i − 1 shifts are required to place the ith element of A in the correct position within B, so the total number of shifts is (^) n ∑
i=
i − 1 =
∑^ n
i=
i −
∑^ n
i=
n(n + 1) 2 −^ n^ =^
n^2 − n
What is the average-case complexity of this algorithm?
Solution Let S be the sample space of possible inputs of size n; that is, each input is a list of n numbers to be sorted. Assume that each possible ordering is equally likely. If X is a random variable whose value for each input is the number of times elements need to be shifted, then the average-case complexity is given by E(X). For each i = 1, 2 ,... , n, let Xi be the random variable whose value is the number of shifts required to place the ith element of A in the proper position in B. Then Xi can assume any value between 0 and i − 1, corresponding to the best case and worst case, respectively. If we assume that
each value of Xi is equally likely, then we have
E(Xi) =
∑^ i−^1
j=
p(Xi = j)j
∑^ i−^1
j=
i j
i
∑^ i−^1
j=
j
i
(i − 1)(i − 1 + 1) 2 =
i
(i − 1)i 2 =
i − 1
Since X = X 1 + X 2 + · · · + Xn, it follows from the linearity of expectations that
E(X) =
∑^ n
i=
E(Xi)
∑^ n
i=
i − 1 2
= (^12)
[ (^) n ∑
i=
i −
∑^ n
i=
n(n + 1) 2 −^ n
= 12 n(n^ + 1) 2 −^2 n
=
n^2 − n
Therefore, the average-case complexity is approximately one-half that of the worst-case complexity. 2
Example Let S = { 1 , 2 , 3 , 4 , 5 , 6 } be the sample space of equally likely outcomes of the roll of a single die, and let X be the random variable defined by X(s) = s for all s ∈ S. What is V (X)?
Solution In the Lecture 15 notes, we computed E(X) = 3.5. To compute V (X), we can use the formula V (X) = E(X^2 ) − E(X)^2
a=
a(21)
a=
a
Since E(X 1 )E(X 2 ) = E(X 1 X 2 ), we can easily compute the variance V (X) as follows:
V (X) = V (X 1 ) + V (X 2 ) =^35 12
It is important to note that the relation E(XY ) = E(X)E(Y ) is a necessary condition for X and Y to be independent, but it is not sufficient. However, this condition is necessary and sufficient for the relation V (X + Y ) = V (X) + V (Y ) to hold, as can be seen from the proof of this formula given in the Lecture 16 notes. 2