algorithmic analysis and sorting - stanford university€¦ · compare algorithms. time-out for...

115
Algorithmic Analysis and Sorting Part One

Upload: others

Post on 17-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Algorithmic Analysis and SortingPart One

Page 2: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Computers use roughly 3% of all the electricity generated in the United States.

This electricity generation produces around 826 megatons of CO₂ each year.

Reducing the need for computing power – or using that power more wisely – could

have a big impact on CO₂ emissions.

Computers use roughly 3% of all the electricity generated in the United States.

This electricity generation produces around 826 megatons of CO₂ each year.

Reducing the need for computing power – or using that power more wisely – could

have a big impact on CO₂ emissions.

Page 3: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Fundamental Question:

How do we measure efficiency?

Page 4: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

One Idea: Runtime

Page 5: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Runtime is Noisy

● Runtime is highly sensitive to which computer you’re using.

● Runtime is highly sensitive to which inputs you’re testing.

● Runtime is highly sensitive to external factors.

Page 6: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

bool linearSearch(const string& str, char ch) { for (int i = 0; i < str.length(); i++) { if (str[i] == ch) { return true; } } return false;}

Work Done: At most k0n + k1

Page 7: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Big-Observations

● If our goal is to extrapolate out the runtime, we don’t need to know the constants in advance. We can figure them out by running the code.

● For “sufficiently large” inputs, only the dominant term matters.● For both 4n + 1000 and n + 137, for very

large n most of the runtime is explained by n.● Is there a concise way of describing this?

Page 8: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Big-Observations

Page 9: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Big-ObservationsNotation

● Ignore everything except the dominant growth term, including constant factors.

● Examples:● 4n + 4 = O(n)● 137n + 271 = O(n)● n2 + 3n + 4 = O(n2)● 2n + n3 = O(2n)

For the mathematically inclined:

f(n) = O(g(n)) if∃n₀ ∈ . ∃ℝ. ∃ c ∈ . ∀ℝ. ∃ n ≥ n₀. f(n) ≤ c|g(n)|

For the mathematically inclined:

f(n) = O(g(n)) if∃n₀ ∈ . ∃ℝ. ∃ c ∈ . ∀ℝ. ∃ n ≥ n₀. f(n) ≤ c|g(n)|

Page 10: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Algorithmic Analysis with Big-O

Page 11: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Algorithmic Analysis with Big-O

double average(const Vector<int>& vec) { double total = 0.0; for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

Page 12: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Algorithmic Analysis with Big-O

double average(const Vector<int>& vec) { double total = 0.0; for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

Page 13: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Algorithmic Analysis with Big-O

double average(const Vector<int>& vec) { double total = 0.0; for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

O(n)

O(n) means “the runtime is proportional to the size of the input.” We’d say that this code

runs in linear time.

O(n) means “the runtime is proportional to the size of the input.” We’d say that this code

runs in linear time.

Page 14: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

A More Interesting Example

Page 15: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

A More Interesting Example

bool linearSearch(const string& str, char ch) { for (int i = 0; i < str.length(); i++) { if (str[i] == ch) { return true; } } return false;}

How do we analyze this?

Page 16: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Types of Analysis

● Worst-Case Analysis● What's the worst possible runtime for the algorithm?● Useful for “sleeping well at night.”

● Best-Case Analysis● What's the best possible runtime for the algorithm?● Useful to see if the algorithm performs well in some

cases.● Average-Case Analysis

● What's the average runtime for the algorithm?● Far beyond the scope of this class; take CS109,

CS161, or CS265 for more information!

Page 17: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Types of Analysis

● Worst-Case Analysis● What's the worst possible runtime for the algorithm?● Useful for “sleeping well at night.”

Best-Case Analysis

What's the best possible runtime for the algorithm?

Useful to see if the algorithm performs well in some cases.

Average-Case Analysis

What's the average runtime for the algorithm?

Far beyond the scope of this class; take CS109, CS161, CS365, or CS369N for more information!

Page 18: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Being Pessimistic

bool linearSearch(const string& str, char ch) { for (int i = 0; i < str.length(); i++) { if (str[i] == ch) { return true; } } return false;}

Worst-Case Runtime: O(n)

Page 19: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Types of Analysis

● Worst-Case Analysis● What's the worst possible runtime for the algorithm?● Useful for “sleeping well at night.”

● Best-Case Analysis● What's the best possible runtime for the algorithm?● Useful to see if the algorithm performs well in some

cases.● Average-Case Analysis

● What's the average runtime for the algorithm?● Far beyond the scope of this class; take CS109,

CS161, or CS265 for more information!

Page 20: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Types of Analysis

Worst-Case Analysis

What's the worst possible runtime for the algorithm?

Useful for “sleeping well at night.”● Best-Case Analysis

● What's the best possible runtime for the algorithm?● Useful to see if the algorithm performs well in some

cases.

Average-Case Analysis

What's the average runtime for the algorithm?

Far beyond the scope of this class; take CS109, CS161, or CS265 for more information!

Page 21: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Three Cheers for Optimism!

bool linearSearch(const string& str, char ch) { for (int i = 0; i < str.length(); i++) { if (str[i] == ch) { return true; } } return false;}

Best-Case Runtime: O(1)

O(1) means “the runtime doesn’t depend on the size of the input.” In

the best case, this code runs in constant time.

O(1) means “the runtime doesn’t depend on the size of the input.” In

the best case, this code runs in constant time.

Page 22: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

What Can Big-O Tell Us?

● Long-term behavior of a function.● If algorithm A has runtime O(n) and

algorithm B has runtime O(n2), for very large inputs algorithm A will always be faster.

● If algorithm A has runtime O(n), for large inputs, doubling the size of the input doubles the runtime.

Page 23: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

What Can't Big-O Tell Us?

● The actual runtime of a function.● 10100n = O(n)● 10-100n = O(n)

● How a function behaves on small inputs.● n3 = O(n3)● 106 = O(1)

Page 24: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Some Standard Runtime Complexities

Page 25: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

0

2

4

6

8

10

12

14

16

Growth Rates, Part I

· O(1)· O(log n)· O(n)

· O(1)· O(log n)· O(n)

Page 26: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

0

50

100

150

200

250

· O(n)· O(n log n)· O(n²)

· O(n)· O(n log n)· O(n²)

Growth Rates, Part II

What is this strange n log n? Stay tuned!

What is this strange n log n? Stay tuned!

Page 27: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

· O(n²)· O(n³)· O(2ⁿ)

· O(n²)· O(n³)· O(2ⁿ)

Growth Rates, Part III

Page 28: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

· O(1)· O(log n)· O(n)· O(n log n)· O(n²)· O(n³)· O(2ⁿ)

· O(1)· O(log n)· O(n)· O(n log n)· O(n²)· O(n³)· O(2ⁿ)

All Together Now!

Exponential runtimes are

scary! Avoid them if at all possible.

Exponential runtimes are

scary! Avoid them if at all possible.

Page 29: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Size 1 log₂ n n n log₂ n n² n³ 2ⁿ

1000 1ns 9.966ns 1μss 9.966μss 1ms 1s 3.4×10284 yr

2000 1ns 10.966ns 2μss 21.932μss 4ms 8s Just… wow.

3000 1ns 11.551ns 3μss 34.652μss 9ms 27s

4000 1ns 11.966ns 4μss 47.863μss 16ms 1.067min

5000 1ns 12.288ns 5μss 61.439μss 25ms 2.083min

6000 1ns 12.551ns 6μss 75.304μss 36ms 3.6min

7000 1ns 12.773ns 7μss 89.412μss 49ms 5.717min

8000 1ns 12.966ns 8μss 103.726μss 64ms 8.533min

9000 1ns 13.136ns 9μss 118.221μss 81ms 12.15min

10000 1ns 13.288ns 10μss 132.877μss 100ms 16.667min

11000 1ns 13.425ns 11μss 147.677μss 121ms 22.183min

12000 1ns 13.551ns 12μss 162.609μss 144ms 28.8min

13000 1ns 13.666ns 13μss 177.661μss 169ms 36.617min

14000 1ns 13.773ns 14μss 192.824μss 196ms 45.733min

Comparison of Runtimes(assuming 1 operation = 1 nanosecond)

Page 30: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

The Story So Far

● Big-O notation is a quantitative measure of how a function’s runtime scales.

● It ignores constants and lower-order terms. Only the fastest-growing terms matter.

● Big-O notation lets us predict how long a function will take to run.

● Big-O notation lets us quantitatively compare algorithms.

Page 31: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Time-Out for Announcements!

Page 32: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Programming Assignments

● Assignment 3 is due on Wednesday.● If you’re following our timetable, you should

be done with the Sierpinski triangle, Human Pyramids, and Shift Scheduling at this point and should be working on Riding Circuit.

● Have questions? Stop by the LaIR, email your section leader, or visit Piazza!

● Assignment 4 will go out on Wednesday.● We’ll be holding YEAH Hours for this

assignment this Wednesday at 7:00PM in room 380-380Y.

Page 33: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

big-Onward!

Page 34: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Sorting Algorithms

Page 35: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

What is sorting?

Page 36: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

One style of “sorting,” but not

the one we’re thinking about...

One style of “sorting,” but not

the one we’re thinking about...

Page 37: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Problem: Given a list of data points, sort those data points

into ascending / descending order by some quantity.

Page 38: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Suppose we want to rearrange a sequence to put elements into ascending order.

What are some strategies we could use?

How do those strategies compare?

Is there a “best” strategy?

Page 39: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

Page 40: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

7 2 1 64

Page 41: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

7 2 1 64

Page 42: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

7 2 1 64

Page 43: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

7 2 1 64

Page 44: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 45: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 46: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 47: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 48: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 49: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 50: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 674

Page 51: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 674

Page 52: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 674

Page 53: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 674

Page 54: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 55: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 64 7

Page 56: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 674

Page 57: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 1 674

Page 58: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 6741

Page 59: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 6741

Page 60: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 6741

Page 61: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 6741

Page 62: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 641 7

Page 63: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

An Initial Idea: Insertion Sort

2 641 7

Page 64: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

/** * Sorts the specified vector using insertion sort. * * @param v The vector to sort. */void insertionSort(Vector<int>& v) { for (int i = 0; i < v.size(); i++) { /* Scan backwards until either (1) there is no * preceding element or the preceding element is * no bigger than us. */ for (int j = i - 1; j >= 0; j--) { if (v[j] <= v[j + 1]) break;

/* Swap this element back one step. */ swap(v[j], v[j + 1]); } }}

Page 65: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 66: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 67: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 68: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 69: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 70: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 71: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 72: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 73: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 74: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 75: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 76: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 77: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 78: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Page 79: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

2 641 7

Work done: O(n)

Page 80: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 17 2

Page 81: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 17 2

Page 82: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 17 2

Page 83: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 17 2

Page 84: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 127

Page 85: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 127

Page 86: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 127

Page 87: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 127

Page 88: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 127

Page 89: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 4 127

Page 90: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1274

Page 91: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1274

Page 92: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1274

Page 93: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1274

Page 94: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 174 2

Page 95: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 174 2

Page 96: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 174 2

Page 97: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 174 2

Page 98: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1742

Page 99: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1742

Page 100: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1742

Page 101: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 1742

Page 102: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 742 1

Page 103: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 742 1

Page 104: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 742 1

Page 105: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 742 1

Page 106: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 742 1

Page 107: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 742 1

Page 108: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 7421

Page 109: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 7421

Page 110: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

How Fast is Insertion Sort?

6 7421

Work Done: 1 + 2 + 3 + 4

Page 111: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

If we run insertion sort on a sequence of n elements, we might have to do

1 + 2 + 3 + 4 + … + (n – 2) + (n – 1)

swaps. How many swaps is this?

Page 112: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

1 + 2 + 3 + … + (n – 2) + (n – 1)

n – 1

n

= n(n – 1) / 2

Page 113: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

The Complexity of Insertion Sort

● In the worst case, insertion sort takes time

= O(n (n – 1) / 2)

= O(n (n – 1))

= O(n2 – n)

= O(n2).● Fun fact: Insertion sorting an array of

random values takes, on average, O(n2) time.● Curious why? Come talk to me after class!

Page 114: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Thinking About O(n2)

14 6 3 9 7 16 2 15

T(n)

14 6 3 9 7 16 2 15 5 10 8 11 1 13 12 4

T(2n) ≈ 4T(n)

Page 115: Algorithmic Analysis and Sorting - Stanford University€¦ · compare algorithms. Time-Out for Announcements! ... “sorting,” but not the one we’re thinking about... One style

Next Time

● Faster Sorting Algorithms● Can you beat O(n2) time?

● Hybrid Sorting Algorithms● When might insertion sort be useful?