Top Banner
Sorting Conclusions David Kauchak cs302 Spring 2013
31

Sorting Conclusions David Kauchak cs302 Spring 2013.

Jan 04, 2016

Download

Documents

Dorothy Roberts
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sorting Conclusions David Kauchak cs302 Spring 2013.

Sorting Conclusions

David Kauchak

cs302

Spring 2013

Page 2: Sorting Conclusions David Kauchak cs302 Spring 2013.

Administrative Find a partner to bounce ideas off of Assignment 2

Proofs should be very concrete Proving big-O, must satisfy definition

When comparing (i.e. ordering) functions you’re not sure about, set them equal to eachother and do some manipulations e.g. compare n and 3^{\sqrt{\log n}} or n^n and 3^{3^n}

Be careful of things that seem “too good to be true”, e.g. a new O(n log n) sorting algorithm that you’ve never heard of

Page 3: Sorting Conclusions David Kauchak cs302 Spring 2013.

Administrivia continued

Assignment 3 Substitution method is a proof by induction. Follow the steps we

talked about before. Make sure to prove/justify any non-trivial steps (e.g. showing big-

O, Ω and θ) If you’re stuck on an algorithm, brainstorm different possibilities

Even start with the naïve algorithm and think about where it’s inefficient

Latex/writeups Use vertical space to help make your argument/steps more clear Look at what is being generated and make sure it looks like you

expect (e.g. n^2.5 vs. n^{2.5} or n^1+\epsilon vs n^{1 + \epsilon}

Page 4: Sorting Conclusions David Kauchak cs302 Spring 2013.

Sorting bounds

Mergsort is O(n log n)

Quicksort is O(n log n) on average

Can we do better?

Page 5: Sorting Conclusions David Kauchak cs302 Spring 2013.

Comparison-based sortingSorted order is determined based only on a comparison between input elements

A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j]

Do any of the sorting algorithms we’ve looked at use additional information?

No All the algorithms we’ve seen are comparison-based sorting algorithms

Page 6: Sorting Conclusions David Kauchak cs302 Spring 2013.

Comparison-based sortingSorted order is determined based only on a comparison between input elements

A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j]

In Java (and many languages) for a class of objects to be sorted we define a comparator

What does it do?

Page 7: Sorting Conclusions David Kauchak cs302 Spring 2013.

Comparison-based sortingSorted order is determined based only on a comparison between input elements

A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j]

In Java (and many languages) for a class of objects to be sorted we define a comparator

What does it do? Just compares any two elements Useful for comparison-based sorting algorithms

Page 8: Sorting Conclusions David Kauchak cs302 Spring 2013.

Comparison-based sortingSorted order is determined based only on a comparison between input elements

A[i] < A[j] A[i] > A[j] A[i] = A[j] A[i] ≤ A[j] A[i] ≥ A[j]

Can we do better than O(n log n) for comparison based sorting approaches?

Page 9: Sorting Conclusions David Kauchak cs302 Spring 2013.

Decision-tree model Full binary tree representing the comparisons

between elements by a sorting algorithm Internal nodes contain indices to be compared

Leaves contain a complete permutation of the input

Tracing a path from root to leave gives the correct reordering/permutation of the input for an input

1:3

| 1,3,2 |

≤ >

| 1,3,2 |

| 2,1,3 |

[3, 12, 7]

[7, 3, 12]

[3, 7, 12]

[3, 7, 12]

Page 10: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

Page 11: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Page 12: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 12 ≤ 7 or is 12 > 7?

Page 13: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 12 ≤ 7 or is 12 > 7?

Page 14: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 12 ≤ 3 or is 12 > 3?

Page 15: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 12 ≤ 3 or is 12 > 3?

Page 16: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 12 ≤ 3 or is 12 > 3?

Page 17: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 7 ≤ 3 or is 7 > 3?

Page 18: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]

Is 7 ≤ 3 or is 7 > 3?

Page 19: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]3, 2, 1

Page 20: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[12, 7, 3]3, 2, 1[3, 7, 12]

Page 21: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[7, 12, 3]

Page 22: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[7, 12, 3]

Page 23: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[7, 12, 3]

Page 24: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[7, 12, 3]

Page 25: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[7, 12, 3]

Page 26: Sorting Conclusions David Kauchak cs302 Spring 2013.

A decision tree model

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

[7, 12, 3] [3, 7, 12]

3, 1, 2

Page 27: Sorting Conclusions David Kauchak cs302 Spring 2013.

How many leaves are in a decision tree?

Leaves must have all possible permutations of the input

What if decision tree model didn’t?

Some input would exist that didn’t have a correct reordering

Input of size n, n! leaves

Page 28: Sorting Conclusions David Kauchak cs302 Spring 2013.

A lower bound

What is the worst-case number of comparisons for a tree?

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

Page 29: Sorting Conclusions David Kauchak cs302 Spring 2013.

A lower bound

The longest path in the tree, i.e. the height

1:2≤ >

2:3≤ >

1:3≤ >

|1,2,3|1:3

≤ >2:3

≤ >

|1,3,2| |3,1,2| |2,3,1| |3,2,1|

|2,1,3|

Page 30: Sorting Conclusions David Kauchak cs302 Spring 2013.

A lower boundWhat is the maximum number of leaves a binary tree of height h can have?

A complete binary tree has 2h leaves

!2 nh

!lognh

from hw )log( nnh

Page 31: Sorting Conclusions David Kauchak cs302 Spring 2013.

Can we do better?