Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into...

17
Sorting Lower Bounds n Beating Them

description

Recap Divide and Conquer –Divide and solve the smaller pieces recursively –Solve the big problem using the solutions to the smaller problems.

Transcript of Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into...

Page 1: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

SortingLower Bounds

n Beating Them

Page 2: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Recap • Divide and Conquer

– Know how to break a problem into smaller problems, such that

– Given a solution to the smaller problems we can easily find a solution to the bigger problem

Page 3: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Recap• Divide and Conquer

– Divide and solve the smaller pieces recursively

– Solve the big problem using the solutions to the smaller problems.

Page 4: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Recap• The Quicksort algorithm:

– chooses an element, called pivot.– Splits the input into three groups:

• smaller than the pivot• equal to pivot• larger than pivot.

– Recursively sort the smaller and the larger groups independently

Page 5: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

How fast can we sort?• Can we do better than O(n lg n)?

• Well… it depends?

• It depends on the model of computation!

Page 6: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Comparison sorting• Only comparisons are used to

determine the order of elements• Example

– Selection Sort– Insertion sort– Merge Sort– Quick Sort– Heapsort

Page 7: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Lower Bounds• How do we prove that any

algorithm will need cnlogn time in the worst case?

• First Assumption: All these algorithms are allowed to only compare elements in the input. (Say only < ?)

Page 8: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

I choose a permutation

of {1,2,3,…N}

Comparison Sort Game

I ask you yes/no questions.

Time is the number of questions asked in the worst case.

I answer.I determine the permuation.

Page 9: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Decision TreesAnother way of looking at the game

• Example: 3 element sort (a1, a2, a3)

• One yes/no question? (Is x < y?)

a1? a2

“Less than” leads to left branch“Greater than” leads to right branch

Page 10: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Decision Trees• Example: 3 element sort (a1, a2, a3) a1? a2

a2? a3

Page 11: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Decision Trees• Example: 3 element sort (a1, a2, a3)

a2? a3

a1? a2

a1, a2, a3

Page 12: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Decision Trees• Example: 3 element sort (a1, a2, a3)

a2? a3

a1? a2

a1? a3a2? a3

a1? a3

a1, a2, a3a3, a1, a2

a1, a3, a2

a2, a1, a3

a2, a3, a1 a3, a2, a1

Page 13: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Decision Trees• Each leaf contains a

permutation indicating that permutation of order has been established

• A decision tree can model a comparison sort

Page 14: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Decision Trees• It is the tree of all possible

instruction traces• Running time = length of path• Worst case running time =

length of the longest path (height of the tree)

Page 15: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Bounding log N!

N! = 1 × 2 × 3 × … × N/2 × … × N

N factors each at most N.

N/2 factors each at least N/2.

NN

N/2 N/2

N/2 log(N/2) log(N!) N log(N)

= (N log(N)).

Page 16: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Lower bound for comparison sorting

• Theorem: Any decision tree that sorts n elements has height (n lg n)

• Proof: There are n! leaves

• A height h binary tree has no more than 2h nodes and it should be able to resolve all n! permutations. )lg(

lg2

lg2

)!lg(!2

nnh

ncnnnh

nhnh

Page 17: Sorting Lower Bounds n Beating Them. Recap Divide and Conquer Know how to break a problem into smaller problems, such that Given a solution to the smaller.

Optimal Comparison Sorting Algorithms

• Merge sort• Quick sort• Heapsort