searching and sorting · big-o notation big-o notation is a way of quantifying the rate at which...

157
Searching and Sorting Part One

Upload: others

Post on 26-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Searching and SortingPart One

Page 2: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Apply to Section Lead!

Don’t just take my word for it...

Page 3: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Recap from Last Time

Page 4: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Big-O Notation

● Big-O notation is a way of quantifying the rate at which some quantity grows.

● For example:● A square of side length r has area O(r2).● A circle of radius r has area O(r2).● A sphere of radius r has surface area O(r2).● A cube of side length r has surface area O(r2).● A cube of side length r has volume O(r3).● A sphere of radius r has volume O(r3).

Doubling r increases area 4×.Tripling r increases area 9×.

Doubling r increases area 4×.Tripling r increases area 9×.

r2r

3r

Doubling r increases area 4×.Tripling r increases area 9×.

Doubling r increases area 4×.Tripling r increases area 9×.

r2r

3r

A 4A 9A A’ 4A’ 9A’

Page 5: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Big-O Notation

● Big-O notation is a way of quantifying the rate at which some quantity grows.

● For example:● A square of side length r has area O(r2).● A circle of radius r has area O(r2).● A sphere of radius r has surface area O(r2).● A cube of side length r has surface area O(r2).● A cube of side length r has volume O(r3).● A sphere of radius r has volume O(r3).

Doubling r increases area 4×.Tripling r increases area 9×.

Doubling r increases area 4×.Tripling r increases area 9×.

r2r

3r

Doubling r increases area 4×.Tripling r increases area 9×.

Doubling r increases area 4×.Tripling r increases area 9×.

r2r

3r

A 4A 9A A’ 4A’ 9A’

This just says that these quantities grow at the same

relative rates. It does not say that they’re equal!

This just says that these quantities grow at the same

relative rates. It does not say that they’re equal!

Page 6: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Assume any individual statement takes one unitof time to execute. If the input Vector has n elements,

how many time units will this code take to run?

double averageOf(const Vector<int>& vec) { double total = 0.0;

for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

Page 7: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Assume any individual statement takes one unitof time to execute. If the input Vector has n elements,

how many time units will this code take to run?

double averageOf(const Vector<int>& vec) { double total = 0.0;

for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

1

1 n+1 n

n

1

Page 8: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

One possible answer: 3n + 4.

double averageOf(const Vector<int>& vec) { double total = 0.0;

for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

1

1 n+1 n

n

1

Page 9: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

One possible answer: 3n + 4.More useful answer: O(n).

double averageOf(const Vector<int>& vec) { double total = 0.0;

for (int i = 0; i < vec.size(); i++) { total += vec[i]; }

return total / vec.size();}

1

1 n+1 n

n

1

Page 10: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Work Done: O(n2).

void printStars(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << '*' << endl; } }}

Page 11: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Work Done: O(1).

void hmmThatsStrange(int n) { cout << "Mirth and Whimsy" << endl;}

Page 12: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

New Stuff!

Page 13: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Sorting Algorithms

Page 14: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

What is sorting?

Page 15: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

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 16: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

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

into ascending / descending order by some quantity.

Page 17: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

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 18: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

Page 19: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 624 1

Page 20: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 624 1

The smallest element

should go in front.

The smallest element

should go in front.

Page 21: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 624 1

Page 22: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 621 4

Page 23: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 621 4

This element is in the right place now.

This element is in the right place now.

The remaining elements are in no particular order.

The remaining elements are in no particular order.

Page 24: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 621 4

Page 25: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 621 4

Page 26: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 621 4The smallest element of the remaining elements goes at the front of the remaining elements.

The smallest element of the remaining elements goes at the front of the remaining elements.

Page 27: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 621 4

Page 28: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 29: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

These elements are in the right

place now.

These elements are in the right

place now.

The remaining elements are in no particular order.

The remaining elements are in no particular order.

Page 30: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 31: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 32: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

The smallest of these remaining elements

goes at the front of the remaining elements.

The smallest of these remaining elements

goes at the front of the remaining elements.

Page 33: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 34: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

These elements are in the right

place now.

These elements are in the right

place now.

The remaining elements are in no particular order.

The remaining elements are in no particular order.

Page 35: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 36: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 37: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2The smallest of these

elements needs to go at the front of this group of

elements.

The smallest of these elements needs to go at the front of this group of

elements.

Page 38: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

7 641 2

Page 39: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

Page 40: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

These elements are in the right

place now.

These elements are in the right

place now.

The remaining elements are in no particular order.

The remaining elements are in no particular order.

Page 41: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

Page 42: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

Page 43: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

The smallest element from this group needs to go at

the front of the group.

The smallest element from this group needs to go at

the front of the group.

Page 44: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

Page 45: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

An Initial Idea: Selection Sort

741 2 6

These elements are in the right

place now.

These elements are in the right

place now.

Page 46: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Selection Sort

● Find the smallest element and move it to the first position.

● Find the smallest element of what’s left and move it to the second position.

● Find the smallest element of what’s left and move it to the third position.

● Find the smallest element of what’s left and move it to the fourth position.

● (etc.)

Page 47: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

/** * Sorts the specified vector using the selection sort algorithm. * * @param elems The elements to sort. */void selectionSort(Vector<int>& elems) { for (int index = 0; index < elems.size(); index++) { int smallestIndex = indexOfSmallest(elems, index); swap(elems[index], elems[smallestIndex]); }} /** * Given a vector and a starting point, returns the index of the smallest * element in that vector at or after the starting point * * @param elems The elements in question. * @param startPoint The starting index in the vector. * @return The index of the smallest element at or after that point */int indexOfSmallest(const Vector<int>& elems, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < elems.size(); i++) { if (elems[i] < elems[smallestIndex]) { smallestIndex = i; } } return smallestIndex;}

Page 48: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Analyzing Selection Sort

● How much work do we do for selection sort?

● To find the smallest value, we need to look at all n array elements.

● To find the second-smallest value, we need to look at n – 1 array elements.

● To find the third-smallest value, we need to look at n – 2 array elements.

● Work is n + (n – 1) + (n – 2) + … + 1.

Page 49: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

n + (n-1) + ... + 2 + 1

n

n + 1

= n(n+1) / 2

Page 50: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Complexity of Selection Sort

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

= O(n (n + 1))

= O(n2 + n)

= O(n2)

So selection sort runs in time O(n2).

Big-O notation ignores low-order terms. Think

“cost of making n widgets.”

Big-O notation ignores low-order terms. Think

“cost of making n widgets.”

Big-O notation ignores constant factors. (Think

“area of a circle” and “area of a square.”)

Big-O notation ignores constant factors. (Think

“area of a circle” and “area of a square.”)

Page 51: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

n + (n-1) + ... + 2 + 1

n

n

The area of a 2D figure that fits

snugly in an n × n box is O(n2).

The area of a 2D figure that fits

snugly in an n × n box is O(n2).

Page 52: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our theory predicts that the runtime of selection sort is O(n2).

Does that match what we see in practice?

What should we expect to see when we look at a runtime plot?

Page 53: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Another Sorting Algorithm

Page 54: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

Page 55: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

7 2 1 64

Page 56: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

7 2 1 64

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in gray is in no particular

order.

This sequence in gray is in no particular

order.

Page 57: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

7 2 1 64

Insert this element into the blue sequence,

making the blue sequence one element longer.

Insert this element into the blue sequence,

making the blue sequence one element longer.

Page 58: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

7 2 1 64

Page 59: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Page 60: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in gray is in no particular

order.

This sequence in gray is in no particular

order.

Page 61: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Insert this element into the blue sequence,

making the blue sequence one element longer.

Insert this element into the blue sequence,

making the blue sequence one element longer.

Page 62: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Page 63: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Page 64: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Page 65: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 674

Page 66: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 674

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in gray is in no particular

order.

This sequence in gray is in no particular

order.

Page 67: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 674

Insert this element into the blue sequence,

making the blue sequence one element longer.

Insert this element into the blue sequence,

making the blue sequence one element longer.

Page 68: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 674

Page 69: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Page 70: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 64 7

Page 71: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 674

Page 72: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 1 674

Page 73: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 6741

Page 74: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 6741

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in gray is in no particular

order.

This sequence in gray is in no particular

order.

Page 75: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 6741

Insert this element into the blue sequence,

making the blue sequence one element longer.

Insert this element into the blue sequence,

making the blue sequence one element longer.

Page 76: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 6741

Page 77: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 641 7

Page 78: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Our Next Idea: Insertion Sort

2 641 7

This sequence in blue, taken in isolation, is in

sorted order.

This sequence in blue, taken in isolation, is in

sorted order.

There are no more gray elements, so the sequence is sorted!

There are no more gray elements, so the sequence is sorted!

Page 79: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Insertion Sort

● Repeatedly insert an element into a sorted sequence at the front of the array.

● To insert an element, swap it backwards until either● (1) it’s bigger than the element before it, or● (2) it’s at the front of the array.

Page 80: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

/** * 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 81: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 82: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 83: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 84: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 85: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 86: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 87: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 88: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 89: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 90: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 91: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 92: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 93: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 94: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7

Page 95: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

2 641 7Work done: O(n)

Page 96: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 17 2

Page 97: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 17 2

Page 98: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 17 2

Page 99: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 17 2

Page 100: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 127

Page 101: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 127

Page 102: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 127

Page 103: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 127

Page 104: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 127

Page 105: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 4 127

Page 106: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1274

Page 107: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1274

Page 108: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1274

Page 109: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1274

Page 110: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 174 2

Page 111: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 174 2

Page 112: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 174 2

Page 113: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 174 2

Page 114: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1742

Page 115: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1742

Page 116: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1742

Page 117: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 1742

Page 118: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 742 1

Page 119: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 742 1

Page 120: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 742 1

Page 121: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 742 1

Page 122: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 742 1

Page 123: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 742 1

Page 124: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 7421

Page 125: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 7421

Page 126: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How Fast is Insertion Sort?

6 7421

Work Done: 1 + 2 + 3 + … + n= O(n2)

Page 127: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Three Analyses

● 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 128: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Complexity of Insertion Sort

● In the best case (the array is sorted), insertion takes time O(n).

● In the worst case (the array is reverse-sorted), insertion sort takes time 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 129: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

How do selection sort and insertion sort compare against one another?

Page 130: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Building a Better Sorting Algorithm

Page 131: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

n

n

2n

2n

Page 132: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

n / 2

n / 2

n

n

Page 133: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Thinking About O(n2)

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

T(n)

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

T(½n) T(½n)

Page 134: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Thinking About O(n2)

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

T(n)

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

¼T(n) ¼T(n)

Page 135: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Thinking About O(n2)

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

T(n)

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

¼T(n) ¼T(n)

2 · ¼T(n) = ½T(n)

Page 136: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

With an O(n2)-time sorting algorithm, it takes twice as long to sort the whole array as it does to split the array in half and sort

each half.

Can we exploit this?

Page 137: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

Page 138: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7 10

Page 139: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7 10 1 4 6 8 9

Page 140: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7 10 1 4 6 8 9

Page 141: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7 10

1

4 6 8 9

Page 142: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2

3 5 7 10

1

4 6 8 9

Page 143: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3

5 7 10

1

4 6 8 9

Page 144: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3

5 7 10

1 4

6 8 9

Page 145: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5

7 10

1 4

6 8 9

Page 146: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5

7 10

1 4 6

8 9

Page 147: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7

10

1 4 6

8 9

Page 148: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7

10

1 4 6 8

9

Page 149: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7

10

1 4 6 8 9

Page 150: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7 101 4 6 8 9

Page 151: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

2 3 5 7 101 4 6 8 9

Each step makes a single comparison and reduces the number of elements

by one.

If there are n total elements, this algorithm

runs in time O(n).

Each step makes a single comparison and reduces the number of elements

by one.

If there are n total elements, this algorithm

runs in time O(n).

Page 152: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

The Key Insight: Merge

● The merge algorithm takes in two sorted lists and combines them into a single sorted list.

● While both lists are nonempty, compare their first elements. Remove the smaller element and append it to the output.

● Once one list is empty, add all elements from the other list to the output.

● Note: Removing the first element of a Vector takes time O(n), so you can’t implement this algorithm by repeatedly removing the first element of one of the Vectors. See if you can find another approach for doing this.

Page 153: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

“Split Sort”

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

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

1. Split the input in half.

2. Insertion sort each half.

3. Merge the halves back together.

1. Split the input in half.

2. Insertion sort each half.

3. Merge the halves back together.

Page 154: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

“Split Sort”

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

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

1. Split the input in half.

2. Insertion sort each half.

3. Merge the halves back together.

1. Split the input in half.

2. Insertion sort each half.

3. Merge the halves back together.

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

Page 155: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

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

“Split Sort”

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

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

1. Split the input in half.

2. Insertion sort each half.

3. Merge the halves back together.

1. Split the input in half.

2. Insertion sort each half.

3. Merge the halves back together.

Page 156: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

“Split Sort”

void splitSort(Vector<int>& v) { /* Split the vector in half */ int half = v.size() / 2; Vector<int> left = v.subList(0, half); Vector<int> right = v.subList(half); /* Sort each half. */ insertionSort(left); insertionSort(right); /* Merge them back together. */ merge(left, right, v);} Prediction: This

should still take time O(n2), but be about

twice as fast as insertion sort.

Prediction: This should still take time O(n2), but be about

twice as fast as insertion sort.

Takes O(n) time, since we copy all n elements into

new Vectors.

Takes O(n) time, since we copy all n elements into

new Vectors.

Takes O(n2) time, but about half as much as what we did before.

Takes O(n2) time, but about half as much as what we did before.

Takes O(n) time.

Takes O(n) time.

Page 157: Searching and Sorting · Big-O Notation Big-O notation is a way of quantifying the rate at which some quantity grows. For example: A square of side length r has area O(r2). A circle

Next Time

● Mergesort● A beautiful, elegant sorting algorithm.

● Analyzing Mergesort● An unusual runtime analysis.

● Hybrid Sorting Algorithms● Improving on mergesort.

● Binary Search● Finding things fast!