the randomized quicksort...

87
Outline The Randomized Quicksort Algorithm K. Subramani 1 1 Lane Department of Computer Science and Electrical Engineering West Virginia University 7 February, 2012 Subramani Sample Analyses

Upload: others

Post on 15-Nov-2019

30 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

Outline

The Randomized Quicksort Algorithm

K. Subramani1

1Lane Department of Computer Science and Electrical EngineeringWest Virginia University

7 February, 2012

Subramani Sample Analyses

Page 2: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

Outline

Outline

1 The Randomized Quicksort Algorithm

Subramani Sample Analyses

Page 3: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Sorting Problem

Problem Statement

Given an array A of n distinct integers, in the indices A[1] through A[n],

Subramani Sample Analyses

Page 4: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Sorting Problem

Problem Statement

Given an array A of n distinct integers, in the indices A[1] through A[n], permute the elements ofA, so that

Subramani Sample Analyses

Page 5: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Sorting Problem

Problem Statement

Given an array A of n distinct integers, in the indices A[1] through A[n], permute the elements ofA, so that A[1] < A[2] . . .A[n].

Subramani Sample Analyses

Page 6: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Sorting Problem

Problem Statement

Given an array A of n distinct integers, in the indices A[1] through A[n], permute the elements ofA, so that A[1] < A[2] . . .A[n].

Note

The assumption of distinctness simplifies the analysis.

Subramani Sample Analyses

Page 7: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Sorting Problem

Problem Statement

Given an array A of n distinct integers, in the indices A[1] through A[n], permute the elements ofA, so that A[1] < A[2] . . .A[n].

Note

The assumption of distinctness simplifies the analysis. It has no bearing on the running time.

Subramani Sample Analyses

Page 8: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Partition subroutine

Function PARTITION(A,p,q)

1: {We partition the sub-array A[p,p+1, . . . ,q] about A[p].}2: for (i = (p +1) to q) do3: if (A[i] < A[p]) then4: Insert A[i] into bucket L.5: else6: if (A[i] > A[p]) then7: Insert A[i] into bucket U.8: end if9: end if

10: end for11: Copy A[p] into A[(|L|+1)].12: Copy the elements of L into the first |L| entries of A[p · ·q].13: Copy A[p] into A[(|L|+1)].14: Copy the elements of U into the entries of A[(|L|+2) · ·q].15: return (|L|+1).

Subramani Sample Analyses

Page 9: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Partition subroutine

Function PARTITION(A,p,q)

1: {We partition the sub-array A[p,p+1, . . . ,q] about A[p].}2: for (i = (p +1) to q) do3: if (A[i] < A[p]) then4: Insert A[i] into bucket L.5: else6: if (A[i] > A[p]) then7: Insert A[i] into bucket U.8: end if9: end if

10: end for11: Copy A[p] into A[(|L|+1)].12: Copy the elements of L into the first |L| entries of A[p · ·q].13: Copy A[p] into A[(|L|+1)].14: Copy the elements of U into the entries of A[(|L|+2) · ·q].15: return (|L|+1).

Note

Partitioning an array can be achieved in linear time.

Subramani Sample Analyses

Page 10: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Quicksort Algorithm

Subramani Sample Analyses

Page 11: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Quicksort Algorithm

Function QUICKSORT(A, p, q)

1: if (p ≥ q) then2: return3: else4: j =PARTITION(A, p, q).5: Quicksort(A, p, j −1).6: Quicksort(A, j +1, q).7: end if

Subramani Sample Analyses

Page 12: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

The Quicksort Algorithm

Function QUICKSORT(A, p, q)

1: if (p ≥ q) then2: return3: else4: j =PARTITION(A, p, q).5: Quicksort(A, p, j −1).6: Quicksort(A, j +1, q).7: end if

Note

The main program calls QUICKSORT(A, 1, n).

Subramani Sample Analyses

Page 13: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Worst-case analysis

Analysis

What is the worst-case input for QUICKSORT()?

Subramani Sample Analyses

Page 14: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Worst-case analysis

Analysis

What is the worst-case input for QUICKSORT()? How many comparisons in the worst case?

Subramani Sample Analyses

Page 15: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Worst-case analysis

Analysis

What is the worst-case input for QUICKSORT()? How many comparisons in the worst case?O(n2).

Subramani Sample Analyses

Page 16: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Worst-case analysis

Analysis

What is the worst-case input for QUICKSORT()? How many comparisons in the worst case?O(n2).

Intuition for randomized case

What sort of assumptions are reasonable in analysis?

Subramani Sample Analyses

Page 17: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Randomized Quicksort

Subramani Sample Analyses

Page 18: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Randomized Quicksort

Function RANDOMIZED-QUICKSORT(A, p, q)

1: if (p ≥ q) then2: return3: else4: Choose a number, say r , uniformly and at random from the set {p,p +1, . . . ,q}.5: Swap A[p] and A[r ].6: j =PARTITION(A, p, q).7: Quicksort(A, p, j −1).8: Quicksort(A, j +1, q).9: end if

Subramani Sample Analyses

Page 19: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Randomized Quicksort

Function RANDOMIZED-QUICKSORT(A, p, q)

1: if (p ≥ q) then2: return3: else4: Choose a number, say r , uniformly and at random from the set {p,p +1, . . . ,q}.5: Swap A[p] and A[r ].6: j =PARTITION(A, p, q).7: Quicksort(A, p, j −1).8: Quicksort(A, j +1, q).9: end if

Note

Worst case running time?

Subramani Sample Analyses

Page 20: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Randomized Quicksort

Function RANDOMIZED-QUICKSORT(A, p, q)

1: if (p ≥ q) then2: return3: else4: Choose a number, say r , uniformly and at random from the set {p,p +1, . . . ,q}.5: Swap A[p] and A[r ].6: j =PARTITION(A, p, q).7: Quicksort(A, p, j −1).8: Quicksort(A, j +1, q).9: end if

Note

Worst case running time? O(n2)!

Subramani Sample Analyses

Page 21: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Randomized Quicksort

Function RANDOMIZED-QUICKSORT(A, p, q)

1: if (p ≥ q) then2: return3: else4: Choose a number, say r , uniformly and at random from the set {p,p +1, . . . ,q}.5: Swap A[p] and A[r ].6: j =PARTITION(A, p, q).7: Quicksort(A, p, j −1).8: Quicksort(A, j +1, q).9: end if

Note

Worst case running time? O(n2)! However, for a randomized algorithm we are not interested inworst-case running time, but in expected running time.

Subramani Sample Analyses

Page 22: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Subramani Sample Analyses

Page 23: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node.

Subramani Sample Analyses

Page 24: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree.

Subramani Sample Analyses

Page 25: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order.

Subramani Sample Analyses

Page 26: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree?

Subramani Sample Analyses

Page 27: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n).

Subramani Sample Analyses

Page 28: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T .

Subramani Sample Analyses

Page 29: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.

Subramani Sample Analyses

Page 30: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.

Subramani Sample Analyses

Page 31: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array?

Subramani Sample Analyses

Page 32: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T .

Subramani Sample Analyses

Page 33: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o.

Subramani Sample Analyses

Page 34: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o. Given an intenal

node o of T , what is the probability that it is good?

Subramani Sample Analyses

Page 35: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o. Given an intenal

node o of T , what is the probability that it is good? At least 12 !

Subramani Sample Analyses

Page 36: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o. Given an intenal

node o of T , what is the probability that it is good? At least 12 !

Consider a root to leaf path in T .

Subramani Sample Analyses

Page 37: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o. Given an intenal

node o of T , what is the probability that it is good? At least 12 !

Consider a root to leaf path in T . How many good nodes can exist on such a path?

Subramani Sample Analyses

Page 38: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o. Given an intenal

node o of T , what is the probability that it is good? At least 12 !

Consider a root to leaf path in T . How many good nodes can exist on such a path? At mostr = log 4

3n.

Subramani Sample Analyses

Page 39: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis

Decision Tree

The operation of RANDOMIZED QUICKSORT() can be thought of as a binary tree, say T , with apivot being chosen at each internal node. The elements in the node which are less than the pivotare shunted to the left subtree and the rest of the elements (excluding the pivot) are shunted tothe right subtree. An in-order traversal of T focusing on the pivots, gives the sorted order. Whatis the work done at each level of the tree? O(n). Let h denote the height of T . Observe that h isa random variable and we are interested in its expected value.The rank of an element of A is its position in A, when A has been sorted.When you pick an element at random, what is the probability that the rank of the element chosenis between 1

4 ·n and 34 ·n, where n is the number of elements in the array? 1

2 .Consider the tree T . We define an internal node o of the tree to be good, if both its children haveat most 3

4 · |o| nodes, where |o| denotes the number of elements in the node o. Given an intenal

node o of T , what is the probability that it is good? At least 12 !

Consider a root to leaf path in T . How many good nodes can exist on such a path? At mostr = log 4

3n. What is the expected number of nodes on a root to leaf path before you see r good

nodes?

Subramani Sample Analyses

Page 40: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p.

Subramani Sample Analyses

Page 41: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads?

Subramani Sample Analyses

Page 42: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Subramani Sample Analyses

Page 43: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is

Subramani Sample Analyses

Page 44: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is r12

=

Subramani Sample Analyses

Page 45: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is r12

= 2 · r =

Subramani Sample Analyses

Page 46: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is r12

= 2 · r = 2 · log 43

n.

Subramani Sample Analyses

Page 47: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is r12

= 2 · r = 2 · log 43

n. However, this

is the expected height of T , i.e., E[h].

Subramani Sample Analyses

Page 48: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is r12

= 2 · r = 2 · log 43

n. However, this

is the expected height of T , i.e., E[h]. Therefore, the expected work undertaken by the algorithm

Subramani Sample Analyses

Page 49: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Decision Tree Analysis (contd.)

Lemma

Consider a coin for which the probability of “heads” turning up on a toss is p. What is theexpected number of tosses to obtain k heads? k

p .

Decision Tree (contd.)

Thus the expected number of nodes on a root to leaf path is r12

= 2 · r = 2 · log 43

n. However, this

is the expected height of T , i.e., E[h]. Therefore, the expected work undertaken by the algorithm

E[h]×work done per level = O(n · logn).

Subramani Sample Analyses

Page 50: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Subramani Sample Analyses

Page 51: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent,

Subramani Sample Analyses

Page 52: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Subramani Sample Analyses

Page 53: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Note

We recall that the rank of an array element is its position in the sorted array.

Subramani Sample Analyses

Page 54: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Note

We recall that the rank of an array element is its position in the sorted array. Every element of Ahas a unique rank in the set {1,2, . . . ,n}.

Subramani Sample Analyses

Page 55: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Note

We recall that the rank of an array element is its position in the sorted array. Every element of Ahas a unique rank in the set {1,2, . . . ,n}.

Analysis

Let S(i) denote the element in A, whose rank i.

Subramani Sample Analyses

Page 56: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Note

We recall that the rank of an array element is its position in the sorted array. Every element of Ahas a unique rank in the set {1,2, . . . ,n}.

Analysis

Let S(i) denote the element in A, whose rank i. We wish to compute the number of comparisonsbetween A[i] and the other elements of A, for each i = 1,2 . . . n.

Subramani Sample Analyses

Page 57: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Note

We recall that the rank of an array element is its position in the sorted array. Every element of Ahas a unique rank in the set {1,2, . . . ,n}.

Analysis

Let S(i) denote the element in A, whose rank i. We wish to compute the number of comparisonsbetween A[i] and the other elements of A, for each i = 1,2 . . . n. Instead, we will compute thenumber of comparisons between S(i) and the elements of other ranks, for each i = 1,2, . . . ,n.

Subramani Sample Analyses

Page 58: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis

Definition

A random variable is an indicator variable, if it assumes the value 1, for the occurrence of someevent, and 0 otherwise.

Note

We recall that the rank of an array element is its position in the sorted array. Every element of Ahas a unique rank in the set {1,2, . . . ,n}.

Analysis

Let S(i) denote the element in A, whose rank i. We wish to compute the number of comparisonsbetween A[i] and the other elements of A, for each i = 1,2 . . . n. Instead, we will compute thenumber of comparisons between S(i) and the elements of other ranks, for each i = 1,2, . . . ,n.Are the two computations equivalent?

Subramani Sample Analyses

Page 59: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Subramani Sample Analyses

Page 60: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm.

Subramani Sample Analyses

Page 61: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm. Clearly,

X =

Subramani Sample Analyses

Page 62: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm. Clearly,

X =n−1

∑i=1

∑j>i

Xij

Subramani Sample Analyses

Page 63: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm. Clearly,

X =n−1

∑i=1

∑j>i

Xij

=n−1

∑i=1

n

∑j=i+1

Xij

Subramani Sample Analyses

Page 64: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm. Clearly,

X =n−1

∑i=1

∑j>i

Xij

=n−1

∑i=1

n

∑j=i+1

Xij

How to compute X?

Subramani Sample Analyses

Page 65: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm. Clearly,

X =n−1

∑i=1

∑j>i

Xij

=n−1

∑i=1

n

∑j=i+1

Xij

How to compute X? We are not interested in X , but in E[X ]!

Subramani Sample Analyses

Page 66: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

Let Xij denote an indicator random variable, defined as follows:

Xij =

{

1, if S(i) and S(j) are compared during the course of the algorithm0, otherwise

Let X denote the total number of comparisons made by the algorithm. Clearly,

X =n−1

∑i=1

∑j>i

Xij

=n−1

∑i=1

n

∑j=i+1

Xij

How to compute X? We are not interested in X , but in E[X ]! Observe that,

E[X ] = E[n−1

∑i=1

n

∑j=i+1

Xij ]

Subramani Sample Analyses

Page 67: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Subramani Sample Analyses

Page 68: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =

Subramani Sample Analyses

Page 69: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Subramani Sample Analyses

Page 70: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared.

Subramani Sample Analyses

Page 71: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] =

Subramani Sample Analyses

Page 72: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij .

Subramani Sample Analyses

Page 73: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ?

Subramani Sample Analyses

Page 74: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}.

Subramani Sample Analyses

Page 75: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij !

Subramani Sample Analyses

Page 76: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij ! Since all choices are made uniformly andat random, the probability of either S(i) or S(j) being picked before the other elements in Sij isexactly

Subramani Sample Analyses

Page 77: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij ! Since all choices are made uniformly andat random, the probability of either S(i) or S(j) being picked before the other elements in Sij isexactly 2

j−i+1 .

Subramani Sample Analyses

Page 78: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij ! Since all choices are made uniformly andat random, the probability of either S(i) or S(j) being picked before the other elements in Sij isexactly 2

j−i+1 .Therefore,

Subramani Sample Analyses

Page 79: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij ! Since all choices are made uniformly andat random, the probability of either S(i) or S(j) being picked before the other elements in Sij isexactly 2

j−i+1 .Therefore,

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Subramani Sample Analyses

Page 80: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij ! Since all choices are made uniformly andat random, the probability of either S(i) or S(j) being picked before the other elements in Sij isexactly 2

j−i+1 .Therefore,

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

=n−1

∑i=1

n

∑j=i+1

2

j − i +1

Subramani Sample Analyses

Page 81: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

Let pij denote the probability that S(i) and S(j) are compared. Clearly, E[Xij] = pij . How tocompute pij ? Let Sij = {S(i),S(i +1), . . .S(j)}. S(i) and S(j) will be compared only if, eitherone of them is picked before the other elements in Sij ! Since all choices are made uniformly andat random, the probability of either S(i) or S(j) being picked before the other elements in Sij isexactly 2

j−i+1 .Therefore,

E[X ] =n−1

∑i=1

n

∑j=i+1

E[Xij ]

=n−1

∑i=1

n

∑j=i+1

2

j − i +1

=n−1

∑i=1

n−i+1

∑k=2

2

k

Subramani Sample Analyses

Page 82: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Subramani Sample Analyses

Page 83: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] ≤n

∑i=1

n

∑k=1

2

k

Subramani Sample Analyses

Page 84: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] ≤n

∑i=1

n

∑k=1

2

k

= 2 ·n

∑i=1

n

∑k=1

1

k

Subramani Sample Analyses

Page 85: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] ≤n

∑i=1

n

∑k=1

2

k

= 2 ·n

∑i=1

n

∑k=1

1

k

= 2 ·n

∑i=1

Hn

Subramani Sample Analyses

Page 86: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] ≤n

∑i=1

n

∑k=1

2

k

= 2 ·n

∑i=1

n

∑k=1

1

k

= 2 ·n

∑i=1

Hn

= 2 ·n ·Hn

Subramani Sample Analyses

Page 87: The Randomized Quicksort Algorithmcommunity.wvu.edu/~krsubramani/courses/sp12/rand/lecnotes/Quicksort.pdf · The Randomized Quicksort Algorithm K. Subramani1 1Lane Department of Computer

The Randomized Quicksort Algorithm

Indicator Variable Analysis (contd.)

Analysis (contd.)

E[X ] ≤n

∑i=1

n

∑k=1

2

k

= 2 ·n

∑i=1

n

∑k=1

1

k

= 2 ·n

∑i=1

Hn

= 2 ·n ·Hn

∈ O(n · logn)

Subramani Sample Analyses