cfe2 ch12 final

201
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Twelve: Sorting and Searching Slides by Evan Gallagher

Upload: ismailabumuhfouz

Post on 28-May-2015

250 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Twelve: Sorting and Searching

Slides by Evan Gallagher

Page 2: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

• To compare the selection sort and merge sort algorithms• To study the linear search and binary search algorithms• To appreciate that algorithms for the same task can differ

widely in performance• To understand the big-Oh notation• To be able to estimate and compare the performance of

algorithms• To write code to measure the running time of a program

Chapter Goals

Page 3: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

The selection sort algorithm

sorts a sequence by repeatedly finding the smallest element

of the unsorted tail region and moving it to the front.

11 9 17 5 12

Page 4: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

The selection sort algorithm

sorts a sequence by repeatedly finding the smallest element

of the unsorted tail region and moving it to the front.

11 9 17 5 12

Page 5: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

The selection sort algorithm

sorts a sequence by repeatedly finding the smallest element

of the unsorted tail region and moving it to the front.

Now the first element is in the correct place.

5 9 17 11 12

Page 6: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

Now do the same for the second position.Find the minimum in the unsorted tail region.

5 9 17 11 12

Page 7: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

There is no need to swap this time.The minimum in the tail portion is already greater.

So we do nothing,and the first and second elements are in order.

5 9 17 11 12

Page 8: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

There is no need to swap this time.The minimum in the tail portion is already greater.

So we do nothing,and the first and second elements are in order.

5 9 17 11 12

Page 9: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

Repeat this process of finding the minimum in the tail portionand either swapping it into place or doing nothing.

5 9 17 11 12

Page 10: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

Repeat this process of finding the minimum in the tail portionand either swapping it into place or doing nothing.

5 9 17 11 12

Page 11: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

Repeat this process of finding the minimum in the tail portionand either swapping it into place or doing nothing.

5 9 17 11 12

Page 12: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

Repeat this process of finding the minimum in the tail portionand either swapping it into place or doing nothing.

5 9 11 17 12

Page 13: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Selection Sort

Repeat this process of finding the minimum in the tail portionand either swapping it into place or doing nothing.

5 9 11 17 12

Page 14: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Even when the unsorted region only two elements long, keep to the same successful strategy.

5 9 11 17 12

Selection Sort

Page 15: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Even when the unsorted region only two elements long, keep to the same successful strategy.

5 9 11 17 12

Selection Sort

Page 16: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Even when the unsorted region only two elements long, keep to the same successful strategy.

5 9 11 12 17

Selection Sort

Page 17: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Even when the unsorted region only two elements long, keep to the same successful strategy.

5 9 11 12 17

Selection Sort

Page 18: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

That leaves you with an unprocessed region of length 1,but of course a region of length 1 is always sorted.

5 9 11 12 17

Selection Sort

Page 19: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

That leaves you with an unprocessed region of length 1,but of course a region of length 1 is always sorted.

You are done.

5 9 11 12 17

Selection Sort

Page 20: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

#include <cstdlib>#include <ctime>#include <iostream>using namespace std;

/**Gets the position of the smallest element in an array range.@param a the array@param from the beginning of the range@param to the end of the range@return the position of the smallest element inthe range a[from]...a[to]

*/int min_position(int a[], int from, int to){

int min_pos = from;for (int i = from + 1; i <= to; i++){

if (a[i] < a[min_pos]) { min_pos = i; }}return min_pos;

}

ch12/selsort.cpp

Selection Sort

Page 21: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

/**Swaps two integers.@param x the first integer to swap@param y the second integer to swap

*/void swap(int& x, int& y){

int temp = x;x = y;y = temp;

}

ch12/selsort.cpp

Selection Sort

Page 22: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

/**Sorts an array using the selection sort algorithm.@param a the array to sort@param size the number of elements in a

*/void selection_sort(int a[], int size){

int next; // The next position to be set to the minimumfor (next = 0; next < size - 1; next++){

// Find the position of the minimumint min_pos = min_position(a, next, size - 1);if (min_pos != next){ swap(a[min_pos], a[next]);}

}}

ch12/selsort.cpp

Selection Sort

Page 23: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

/**Prints all elements in an array.@param a the array to print@param size the number of elements in a

*/void print(int a[], int size){

for (int i = 0; i < size; i++){ cout << a[i] << " ";}cout << endl;

}

ch12/selsort.cpp

Selection Sort

Page 24: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

int main(){

srand(time(0));const int SIZE = 20;int values[SIZE];for (int i = 0; i < SIZE; i++){ values[i] = rand() % 100;}print(values, SIZE);selection_sort(values, SIZE);print(values, SIZE);return 0;

}

ch12/selsort.cpp

Selection Sort

Page 25: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

If speed was not an issue for us,we could stop the discussion of sorting right here.

However, the selection sort algorithm shows disappointingperformance when run on large data sets,

and it is worthwhile to study better sorting algorithms.

Selection Sort

Page 26: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

.

Page 27: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

To measure the performance of a program,one could simply run it

and measure how long it takes by using a stopwatch.

click

click

Go!

Profiling A Program Run

Page 28: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

However, most of our programs run very quickly,and it is not easy to time them accurately in this way.

click

click

Go!

Profiling A Program Run

Page 29: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Instead we use the time function.

Profiling A Program Run

Page 30: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

#include <ctime>

is required

Profiling A Program Run

Page 31: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

int now = time(0);

This call sets now to the number of secondsthat have elapsed since January 1, 1970.

Profiling A Program Run

Page 32: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

int now = time(0); int later = time(0);

We only care about the differencebetween the start and end times.

Profiling A Program Run

Page 33: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling the Selection Sort Algorithm

By measuring the time just before and after the sorting,

int before = time(0);selection_sort(values, size);int after = time(0);cout << "Elapsed time = “ << after - before << " seconds" << endl;

Page 34: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling the Selection Sort Algorithm

By measuring the time just before and after the sorting,you don’t count the time it takes to initialize the array

or the time during which the program waitsfor the user to provide inputs.

int before = time(0);selection_sort(values, size);int after = time(0);cout << "Elapsed time = “ << after - before << " seconds" << endl;

Page 35: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Here are the results of some sample runs.

Profiling the Selection Sort Algorithm

Page 36: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

These measurements were obtained on a Pentium processorwith a clock speed of 1.67 GHz running Linux.

Profiling the Selection Sort Algorithm

Page 37: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

On another computer, the actual numbers will differ,but the relationship between the numbers will be the same.

Profiling the Selection Sort Algorithm

Page 38: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

As you can see,doubling the size of the data set

more than doubles the time needed to sort it..

Profiling the Selection Sort Algorithm

Page 39: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

Let’s do some analysis…

Page 40: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

Let us count the number of operations that a program must carry out to sort a sequence using the selection sort algorithm.

Actually, we don’t know how many machine operationsare generated for each C++ instruction

or which of those instructions are more time-consuming than others, but we can make a simplification.

Page 41: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

Simply count how often an element is visited.

Each visit requires about the sameamount of work by other operations,

such as incrementing subscriptsand comparing values.

Page 42: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

Let n be the size of the array.

Page 43: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

To set the first element from the unsorted portion of the array:

You must find the smallest of n numbers.This takes n visits.

Then you must (or might) swap the elements.This takes two visits.

Page 44: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

To set the second element from the unsorted portion of the array:

You must find the smallest of n – 1 numbers.This takes n – 1 visits.

Then you must (or might) swap the elements.This takes two visits.

Page 45: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

To set the third element from the unsorted portion of the array:

You must find the smallest of n – 2 numbers.This takes n – 2 visits.

Then you must (or might) swap the elements.This takes two visits.

Page 46: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

The process continues until…

…there are only two elements in the unsorted region.

Page 47: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

To set the last two elements:

There is no searching, there are just two elements.This takes zero visits.

Then you must (or might) swap the last two elements.This takes two visits.

Page 48: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

Therefore, the total number of visits is

Page 49: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

This is a quadratic equation in n.

That explains why the graph looks

approximately like a parabola.

Page 50: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

Now simplify the analysis further:

Just ignore the lower-level terms!

Page 51: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

We are really only interested inhow the algorithm performs

as n increases.

We need only look for the factor thatmost influences the increase in time.

We can discount constant multipliersand look for the largest exponent.

Here the most important factor is the n2

Page 52: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

We will simply say:

“The number of visits is of order n2”.

Computer scientists often use big-Oh notation:

The number of visits is O(n2).

Page 53: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

To turn an exact expression such as

into big-Oh notation,

locate the fastest-growing term, n2,and ignore its constant coefficient,

½ in this case, no matter how large or small it may be:

O(n2)

Page 54: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

The sad fact remains thatdoubling the size of the array causes

a fourfold increase in the time required for sorting it.

When the size of the sequence increases by a factor

of 100, the sorting time increases by a factor of 10,000!

Page 55: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Performance of the Selection Sort Algorithm

To sort a sequence of a million

entries (for example, to create a telephone directory), takes 10,000 times as long

as sorting 10,000 entries.

If 10,000 entries can be sorted in about a second (as in our

example), then sorting one million entries requires almost three hours.

Page 56: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

click

Go!

Profiling A Program Run

Page 57: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 1

Page 58: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 2

Page 59: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 3

Page 60: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 4

Page 61: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 5

Page 62: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 6

Page 63: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 7

Page 64: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 8

Page 65: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 9

Page 66: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 10

Page 67: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 11

Page 68: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 12

Page 69: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Profiling A Program Run

day 13

Page 70: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

click

Profiling A Program Run

day 14

Page 71: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

A Fortnight!

Profiling A Program Run

day 14

Page 72: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Merge Sort

Page 73: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

The basic idea behind merge sort is very simple:

wishful thinking!

Page 74: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

We will simply hope that the first half

of the array is already perfectly sorted,

and the second half is too.

Page 75: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Merge Sort

Page 76: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 77: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 78: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 79: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 80: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 81: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 82: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 83: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 84: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Now it is an easy matter to merge the two sorted sequencesinto a sorted sequence, simply by taking a new element from

either the first or the second subarray andchoosing the smaller of the elements each time:

Page 85: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

This process continuesuntil…

Page 86: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

This process continuesuntil…

Merge Sort

Page 87: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

This process continuesuntil…

There is only one element left (in either side),

Merge Sort

Page 88: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

This process continuesuntil…

There is only one element left (in either side),

Merge Sort

Page 89: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

This process continuesuntil…

and the array is completely sorted.

Merge Sort

Page 90: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

Recall that the basic idea behind merge sort is

wishful thinking!

Page 91: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

The two half arrays will hardly ever be already sorted.

Someone had to sort each half.

Page 92: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

But if you think about it,what really happed was:

the array was divided into halves,then each half was sorted (by magic),

Page 93: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

But if you think about it,what really happed was:

the array was divided into halves,then each half was sorted (by magic),

and then the two sorted halves were merged together.

Page 94: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

If this process were to continue,then eventually there would besubarrays of only one element

which are very easy to sort and merged together

Page 95: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

If this process were to continue,then eventually there would besubarrays of only one element

which are very easy to sort and merged together

and then the two-length subarrays could be merged together

and then the four-length subarrays could be merged together

and then the eight-length subarrays could be merged together

and then the. . .

Page 96: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Merge Sort

and the entire array will be sortedwhen we get back to merging the first two halves:

Merge Sort!

Yes, this is a recursive algorithm.

At the recursive step,the merge sort is applied to each half problem,

recursively sorting each half and putting them back together.

The end test is an array of only one element.

Page 97: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

We pass the same array on each call

and use indicesas the left and right endpointsof the subarray to be sorted.

Merge Sort

0 9

Page 98: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Using 0 and 9 (the whole array),one of the subarrays will be:

5 to 9:

Merge Sort

0 9

Page 99: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Using 0 and 9 (the whole array),one of the subarrays will be:

5 to 9:

Merge Sort

5 9

Page 100: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

The end test of a one element arrayoccurs when the endpoints are the same index:

Merge Sort

77

Page 101: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

7

The end test of a one element arrayoccurs when the endpoints are the same index:

Merge Sort

7

Page 102: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

The end test of a one element arrayoccurs when the endpoints are the same index:

Merge Sort

7

Page 103: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

/**Sorts the elements in a range of an array.@param a is the array with the elements to sort@param from start of the range to sort@param to end of the range to sort

*/void merge_sort(int a[], int from, int to){

if (from == to) { return; }int mid = (from + to) / 2;// Sort the first half and the second halfmerge_sort(a, from, mid);merge_sort(a, mid + 1, to);merge(a, from, mid, to);

}

Merge Sort

Page 104: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

The merging part is longer,but straightforward.

The code for merge sort:

Merge Sort

Page 105: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort#include <cstdlib>#include <ctime>#include <iostream>using namespace std;

/** Merges two adjacent ranges in an array. @param a the array with the elements to merge @param from the start of the first range @param mid the end of the first range @param to the end of the second range*/

void merge(int a[], int from, int mid, int to){ int n = to - from + 1; // Size of the range to be merged

// Merge both halves into a temporary array b . // We allocate the array dynamically because its size is only // known at runtime—see Section 7.4 int* b = new int[n];

Page 106: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

int i1 = from; // Next element to consider in the first half

int i2 = mid + 1; // Next element to consider in the second half

int j = 0; // Next open position in b

Page 107: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

// As long as neither i1 nor i2 is past the end, // move the smaller element into b while (i1 <= mid && i2 <= to) { if (a[i1] < a[i2]) { b[j] = a[i1]; i1++; } else { b[j] = a[i2]; i2++ j++; } }

Page 108: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

// Note that the body of only one of the // two while loops below is executed

// Copy any remaining entries of the first half while (i1 <= mid) { b[j] = a[i1]; i1++; j++; }

// Copy any remaining entries of the second half while (i2 <= to) { b[j] = a[i2]; i2++; j++; }

Page 109: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

// Copy back from the temporary array for (j = 0; j < n; j++) { a[from + j] = b[j]; }

// The temporary array is no longer needed delete[] b;}

Page 110: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

/** Sorts the elements in a range of an array. @param a the array with the elements to sort @param from start of the range to sort @param to end of the range to sort*/void merge_sort(int a[], int from, int to){ if (from == to) { return; } int mid = (from + to) / 2; // Sort the first half and the second half merge_sort(a, from, mid); merge_sort(a, mid + 1, to); merge(a, from, mid, to);}

Page 111: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

/** Prints all elements in an array. @param a the array to print @param size the number of elements in a*/void print(int a[], int size){ for (int i = 0; i < size; i++) { cout << a[i] << " "; } cout << endl;}

Page 112: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

ch12/mergesort.cpp

Merge Sort

int main(){ srand(time(0)); const int SIZE = 20; int values[SIZE]; for (int i = 0; i < SIZE; i++) { values[i] = rand() % 100; } print(values, SIZE); merge_sort(values, 0, SIZE - 1) print(values, SIZE); return 0;}

Page 113: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

.

Page 114: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

The merge sort algorithm looksmuch more complicated

than the selection sort algorithm,

and it appears that it may welltake much longer

to carry out these repeated subdivisions.

Page 115: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Here are the results of some sample runscomparing selection sort and merge sort.

Analyzing the Merge Sort Algorithm

Page 116: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

You’ll need to look closely – there’s merge sort.

Analyzing the Merge Sort Algorithm

Page 117: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Note that the graph does not have a parabolic shape.

Instead, it appears as if the running time grows approximately linearly with the size of the sequence.

Analyzing the Merge Sort Algorithm

Page 118: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To understand why the merge sort algorithmis such a tremendous improvement,

let us estimate the number of sequence element visits.

Page 119: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

First, we tackle the merge process that happens

after the first and second halves have been sorted.

merge(a, from, mid, to);

Page 120: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Each step in the merge process adds one more element to b.

There are n elements in b.

That element may come from the first or second half of a,and in most cases the elements from the two halves

must be compared to see which one to take.

Count that as 3 visits per element(one for b and one each for the two halves of a),

or 3n visits total.

Page 121: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

3n (for comparisons)

Page 122: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Then you must

copy back from b to a.

Using two visits per elementyields another 2n visits.

Page 123: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

3n (for comparisons) 2n (for copying) -------- 5n (for the whole merging process)

Page 124: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Now for the merge sort portion.

merge_sort(a, from, mid);merge_sort(a, mid + 1, to);

Letting T(n) denote the number of visits required to sort a range of n elements,

the two recursive calls will each require T(n/2) visits.

Page 125: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Now for the merge sort portion.

merge_sort(a, from, mid);merge_sort(a, mid + 1, to);

Letting T(n) denote the number of visits required to sort a range of n elements,

the two recursive calls will each require T(n/2) visits.

Page 126: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Now for the merge sort portion.

merge_sort(a, from, mid);merge_sort(a, mid + 1, to);

Letting T(n) denote the number of visits required to sort a range of n elements,

the two recursive calls will each require T(n/2) visits.

But…

Page 127: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

What if n is not an even number?

Page 128: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

What if n is not an even number?

We can (and will) make the assumption thatn is some power of 2, say m

(this will help later on).

n = 2m

Now every subsequence is divisible by 2.

Page 129: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To understand the relationship,

evaluate T(n/2),using the same formula:

Page 130: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To understand the relationship,

evaluate T(n/2),using the same formula:

Page 131: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Or:

Page 132: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Do that process again:

evaluate T(n/4),using the same formula:

Page 133: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Do that process again:

evaluate T(n/4),using the same formula:

Page 134: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Or:

Page 135: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

This generalizes from 2, 4, 8, to arbitrary powers of 2:

Page 136: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Letting k be the power of 2:

Page 137: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Remember that it’s-going-to-be-helpful-laterassumption we made that n was a power of 2?

n = 2m

Page 138: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Let k = m

Page 139: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Let k = m

Page 140: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Let k = m

Page 141: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Let k = m

Page 142: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Let k = mBecause k = 2m, you have m = log2(n).

Page 143: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Let k = mBecause k = 2m, you have m = log2(n).

Page 144: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To establish the growth order,you drop the lower order term n

Page 145: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To establish the growth order,you drop the lower order term n

and you are left with:

Page 146: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To establish the growth order,you drop the lower order term n

and you are left with:

Page 147: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

You will also drop constant term 5:

Page 148: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

You will also drop constant term 5:

Page 149: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

It is customary to drop the base of the logarithmbecause all logarithms are related by a constant factor.

Page 150: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

So the Merge Sort algorithm is an

O( n log( n ) )

algorithm.

So much better than Selection Sort’s O(n2)

How much better?

Page 151: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

To sort 1,000,000 integers,

if it took Selection Sort a bit less than three hours O(n2)

on the same machine,

it would take Merge Sort about three minutes! O( n log( n ) )

Page 152: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Searching

Searching for an element in a sequenceis an extremely common task.

As with sorting,the right choice of algorithmscan make a big difference.

Page 153: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

What’s Bjarne Stroustrup’s phone number?

Here’s the phone book – look it up!

(He’s under the C++’s, not the S’s!)

It’s easy because the elementsin a phone book are sorted by name.

Page 154: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

Whose phone number is 399-728-9011?

Here’s the phone book – look it up!

Not so easy!

Page 155: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

So get started.

Until you find the number:

Open the phone book to the first page,go to the first entry,

then the second entry,then the third…

go through all of them on page 1

if it’s not there, you’ll need to turn the page

Page 156: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

On page 2:

go to the first entry,then the second entry,

then the third…all of them on page 2

if it’s not there, you’ll need to turn the page

Page 157: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

On page 3:

go to the first entry, then the second…

Page 158: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

THIS IS GOING TO TAKE FOREVER!

Or maybe I’ll get lucky.

Page 159: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

This process is called a linear or sequential search.

A linear search examines all values in a sequenceuntil it finds a match or reaches the end.

Here’s the code:

Page 160: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

int linear_search(int a[], int size, int value) { for (int i = 0; i < size; i++) { if (a[i] == value) { return i; } } return -1; }

-1 is returned to indicate that the value was not found.

ch12/lsearch.cpp(excerpt)

Page 161: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Linear Search Algorithm

.

Page 162: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Linear Search Algorithm

If you assume that the value is present in the array, then the average search visits n/2 elements.

In the worst case, when the value it is not present,then all n elements must be inspected to verify the absence.

Either way, a linear search is an O(n) algorithm.

Page 163: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

If you were told that the values in an array were already sorted,

(sounds a little bit like what we assumed at first in the Merge Sort!)

would you still consider using a linear search?

Page 164: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Consider this array:

Page 165: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Is 123 in the array?

Page 166: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Is 123 in the first half of this array?

Page 167: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Consider the last element in the first half (at index 3)?What is the relationship between 123

and the last value in the first half array?

Page 168: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Consider the last element in the first half (at index 3)?What is the relationship between 123

and the last value in the first half array?

?

123

Page 169: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Given that 123 is greater than 100,then because the data is sorted,123 must be in the upper half.

?

123

Page 170: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Given that 123 is greater than 100,then because the data is sorted,123 must be in the upper half.

?

123

Page 171: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Given that 123 is greater than 100,then because the data is sorted,123 must be in the upper half.

?

123

Page 172: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Repeat the process on the upper half array.

123

Page 173: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Consider the last index in the lower halfof the array from index 4 to 7.

That’s index 5:

?

123

Page 174: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

123 is less than 290so it must be in the left half of this subarray

(or not in the array at all)

?

123

Page 175: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

123 is less than 290so it must be in the left half of this subarray

(or not in the array at all)

123

Page 176: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Consider the last index in the lower halfof the very short subarray from index 4 to 5.

That at index 4:

?

123

Page 177: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

123 is greater than 115 so you must look atthe very, very short array index 5:

?

123

Page 178: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

123 is greater than 115 so you must look atthe very, very short array index 5:

?

123

Page 179: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

There’s only one element in this subarrayand 290 is not 123.

123 is not found.

?

123

Page 180: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

There’s only one element in this subarrayand 290 is not 123.

123 is not found.

Not found

123

Page 181: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Did we visit all n elements? No.

Not found

123

Page 182: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Did we visit all n elements? No.

Not found

123

Page 183: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Did we visit all n elements? No.

Binary search cuts the search in half each time.We do not visit every element.

Of course, this is only possible when thevalues in the array already sorted.

Not found

123

Page 184: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Binary Search

Here is the code for binary search:

Page 185: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Linear Search

int binary_search(int a[], int from, int to, int value){ if (from > to) { return -1; }

int mid = (from + to) / 2; if (a[mid] == value) { return mid; } else if (a[mid] < value) { return binary_search(a, mid + 1, to, value); } else { return binary_search(a, from, mid - 1, value); }}

ch12/bsearch.cpp (excerpt)

Page 186: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Binary Search Algorithm

.

Page 187: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Binary Search Algorithm

Use the same technique as in the analysis of merge sortto determine the number of element visits required.

Page 188: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Binary Search Algorithm

Because you look at the middle element,which counts as one comparison,

and then search either the left or the rightsubsequence, you have

Page 189: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

As before, to understand the relationship,

evaluate T(n/2),using the same formula, you get:

Page 190: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

As before, to understand the relationship,

evaluate T(n/2),using the same formula, you get:

Page 191: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Again, evaluate T(n/4),

using the same formula, and continue…

Page 192: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

This generalizes to:

Page 193: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

As in the analysis of merge sort,you make the simplifying assumption

that n is a power of 2, n = 2 m,where m = log2(n).

Then you obtain

Page 194: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

As in the analysis of merge sort,you make the simplifying assumption

that n is a power of 2, n = 2 m,where m = log2(n).

Then you obtain

Page 195: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

So binary search is an O(log(n)) algorithm.

Page 196: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Binary search is much faster than linear search,

but

is it worthwhile to sort a sequence firstand then use a binary search?

Sorting takes time, too!

Page 197: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

Is it worth the time to sort?

It depends.

Page 198: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Analyzing the Merge Sort Algorithm

If you will only search the sequence once,then it is more efficient to pay

for an O(n) linear search than for anO(n log(n)) sort plus an O(log(n)) binary search.

But if one must make a number of searchesin the same sequence,

then sorting before the searchesis definitely worthwhile.

Page 199: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Summary

Page 200: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Chapter Summary

Page 201: Cfe2 ch12 final

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

End Chapter Twelve

Slides by Evan Gallagher