• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Cfe2 ch12 final
 

Cfe2 ch12 final

on

  • 364 views

 

Statistics

Views

Total Views
364
Views on SlideShare
296
Embed Views
68

Actions

Likes
0
Downloads
11
Comments
0

2 Embeds 68

http://cs215.wikispaces.com 66
https://cs215.wikispaces.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Cfe2 ch12 final Cfe2 ch12 final Presentation Transcript

    • Chapter Twelve: Sorting and Searching C++ for Everyone by Cay HorstmannSlides by Evan Gallagher Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Chapter Goals • 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 C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 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. 5 9 17 11 12 Now the first element is in the correct place. C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 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. 5 9 17 11 12 So we do nothing, and the first and second elements are in order. C++ for Everyone by Cay Horstmann Copyright © 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. 5 9 17 11 12 So we do nothing, and the first and second elements are in order. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Repeat this process of finding the minimum in the tail portion and either swapping it into place or doing nothing. 5 9 17 11 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Repeat this process of finding the minimum in the tail portion and either swapping it into place or doing nothing. 5 9 17 11 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Repeat this process of finding the minimum in the tail portion and either swapping it into place or doing nothing. 5 9 17 11 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Repeat this process of finding the minimum in the tail portion and either swapping it into place or doing nothing. 5 9 11 17 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Repeat this process of finding the minimum in the tail portion and either swapping it into place or doing nothing. 5 9 11 17 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Even when the unsorted region only two elements long, keep to the same successful strategy. 5 9 11 17 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Even when the unsorted region only two elements long, keep to the same successful strategy. 5 9 11 17 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Even when the unsorted region only two elements long, keep to the same successful strategy. 5 9 11 12 17 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort Even when the unsorted region only two elements long, keep to the same successful strategy. 5 9 11 12 17 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort 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 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort 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 You are done. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort#include <cstdlib>#include <ctime> ch12/selsort.cpp#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 in the 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort ch12/selsort.cpp/** 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort ch12/selsort.cpp/** 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 minimum for (next = 0; next < size - 1; next++) { // Find the position of the minimum int min_pos = min_position(a, next, size - 1); if (min_pos != next) { swap(a[min_pos], a[next]); } }} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort ch12/selsort.cpp/** 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort ch12/selsort.cppint 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Selection Sort If speed was not an issue for us, we could stop the discussion of sorting right here. However, the selection sort algorithm shows disappointing performance when run on large data sets, and it is worthwhile to study better sorting algorithms. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run . C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run To measure the performance of a program, one could simply run it and measure how long it takes by using a stopwatch. Go! click click C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run However, most of our programs run very quickly, and it is not easy to time them accurately in this way. Go! click click C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run Instead we use the time function. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run #include <ctime> is required C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run int now = time(0); This call sets now to the number of seconds that have elapsed since January 1, 1970. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run int now = time(0); int later = time(0); We only care about the difference between the start and end times. C++ for Everyone by Cay Horstmann Copyright © 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; C++ for Everyone by Cay Horstmann Copyright © 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 waits for the user to provide inputs. int before = time(0); selection_sort(values, size); int after = time(0); cout << "Elapsed time = “ << after - before << " seconds" << endl; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling the Selection Sort Algorithm Here are the results of some sample runs. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling the Selection Sort Algorithm These measurements were obtained on a Pentium processor with a clock speed of 1.67 GHz running Linux. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling the Selection Sort Algorithm On another computer, the actual numbers will differ, but the relationship between the numbers will be the same. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling the Selection Sort Algorithm As you can see, doubling the size of the data set more than doubles the time needed to sort it.. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort Algorithm Let’s do some analysis… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort AlgorithmLet 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 operations are generated for each C++ instruction or which of those instructions are more time-consuming than others, but we can make a simplification. C++ for Everyone by Cay Horstmann Copyright © 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 same amount of work by other operations, such as incrementing subscripts and comparing values. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort Algorithm Let n be the size of the array. C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort AlgorithmTo 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. C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort Algorithm Therefore, the total number of visits is C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort Algorithm We are really only interested in how the algorithm performs as n increases. We need only look for the factor that most influences the increase in time. We can discount constant multipliers and look for the largest exponent. Here the most important factor is the n2 C++ for Everyone by Cay Horstmann Copyright © 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). C++ for Everyone by Cay Horstmann Copyright © 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) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Performance of the Selection Sort Algorithm The sad fact remains that doubling 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! C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run Go! click C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 1 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 2 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 3 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 4 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 5 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 6 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 7 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 8 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 9 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 10 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 11 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 12 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run day 13 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run click day 14 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Profiling A Program Run A Fortnight! day 14 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Merge Sort C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort The basic idea behind merge sort is very simple: wishful thinking! C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Now it is an easy matter to merge the two sorted sequences into a sorted sequence, simply by taking a new element from either the first or the second subarray and choosing the smaller of the elements each time: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort This process continues until… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort This process continues until… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort This process continues until… There is only one element left (in either side), C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort This process continues until… There is only one element left (in either side), C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort This process continues until… and the array is completely sorted. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Recall that the basic idea behind merge sort is wishful thinking! C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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), C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort If this process were to continue, then eventually there would be subarrays of only one element which are very easy to sort and merged together C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort If this process were to continue, then eventually there would be subarrays 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. . . C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort and the entire array will be sorted when 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort We pass the same array on each call and use indices as the left and right endpoints of the subarray to be sorted. 0 9 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Using 0 and 9 (the whole array), one of the subarrays will be: 5 to 9: 0 9 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort Using 0 and 9 (the whole array), one of the subarrays will be: 5 to 9: 5 9 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort The end test of a one element array occurs when the endpoints are the same index: 77 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort The end test of a one element array occurs when the endpoints are the same index: 7 7 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort The end test of a one element array occurs when the endpoints are the same index: 7 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort/** 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 half merge_sort(a, from, mid); merge_sort(a, mid + 1, to); merge(a, from, mid, to);} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort The merging part is longer, but straightforward. The code for merge sort: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort#include <cstdlib>#include <ctime> ch12/mergesort.cpp#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]; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cpp 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 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cpp // 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++; } } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cpp // 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++; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cpp // 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cpp/** 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);} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cpp/** 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Merge Sort ch12/mergesort.cppint 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;} C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm . C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm The merge sort algorithm looks much more complicated than the selection sort algorithm, and it appears that it may well take much longer to carry out these repeated subdivisions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Here are the results of some sample runs comparing selection sort and merge sort. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm You’ll need to look closely – there’s merge sort. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm To understand why the merge sort algorithm is such a tremendous improvement, let us estimate the number of sequence element visits. C++ for Everyone by Cay Horstmann Copyright © 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); C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm 3n (for comparisons) C++ for Everyone by Cay Horstmann Copyright © 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 element yields another 2n visits. C++ for Everyone by Cay Horstmann Copyright © 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) C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm What if n is not an even number? C++ for Everyone by Cay Horstmann Copyright © 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 that n is some power of 2, say m (this will help later on). n = 2m Now every subsequence is divisible by 2. C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Or: C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Or: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm This generalizes from 2, 4, 8, to arbitrary powers of 2: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Letting k be the power of 2: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Remember that it’s-going-to-be-helpful-later assumption we made that n was a power of 2? n = 2m C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Let k = m C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Let k = m C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Let k = m C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Let k = m C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Let k = m Because k = 2m, you have m = log2(n). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Let k = m Because k = 2m, you have m = log2(n). C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm You will also drop constant term 5: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm You will also drop constant term 5: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm It is customary to drop the base of the logarithm because all logarithms are related by a constant factor. C++ for Everyone by Cay Horstmann Copyright © 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? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort AlgorithmTo 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 ) ) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Searching Searching for an element in a sequence is an extremely common task. As with sorting, the right choice of algorithms can make a big difference. C++ for Everyone by Cay Horstmann Copyright © 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 elements in a phone book are sorted by name. C++ for Everyone by Cay Horstmann Copyright © 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! C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Linear Search On page 3: go to the first entry, then the second… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Linear SearchTHIS IS GOING TO TAKE FOREVER! Or maybe I’ll get lucky. C++ for Everyone by Cay Horstmann Copyright © 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 sequence until it finds a match or reaches the end. Here’s the code: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Linear Search ch12/lsearch.cpp (excerpt)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. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Linear Search Algorithm . C++ for Everyone by Cay Horstmann Copyright © 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. C++ for Everyone by Cay Horstmann Copyright © 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? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Consider this array: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Is 123 in the array? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Is 123 in the first half of this array? C++ for Everyone by Cay Horstmann Copyright © 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? C++ for Everyone by Cay Horstmann Copyright © 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 ? C++ for Everyone by Cay Horstmann Copyright © 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 ? C++ for Everyone by Cay Horstmann Copyright © 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 ? C++ for Everyone by Cay Horstmann Copyright © 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 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Repeat the process on the upper half array. 123 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Consider the last index in the lower half of the array from index 4 to 7. That’s index 5: 123 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search 123 is less than 290 so it must be in the left half of this subarray (or not in the array at all) 123 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search 123 is less than 290 so it must be in the left half of this subarray (or not in the array at all) 123 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Consider the last index in the lower half of the very short subarray from index 4 to 5. That at index 4: 123 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search 123 is greater than 115 so you must look at the very, very short array index 5: 123 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search 123 is greater than 115 so you must look at the very, very short array index 5: 123 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search There’s only one element in this subarray and 290 is not 123. 123 is not found. 123 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search There’s only one element in this subarray and 290 is not 123. 123 is not found. 123 Not found C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Did we visit all n elements? No. 123 Not found C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Did we visit all n elements? No. 123 Not found C++ for Everyone by Cay Horstmann Copyright © 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 the values in the array already sorted. 123 Not found C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Binary Search Here is the code for binary search: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Linear Search ch12/bsearch.cpp (excerpt)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); } C++ for Everyone by Cay Horstmann} Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Binary Search Algorithm . C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Binary Search Algorithm Use the same technique as in the analysis of merge sort to determine the number of element visits required. C++ for Everyone by Cay Horstmann Copyright © 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 right subsequence, you have C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 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: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Again, evaluate T(n/4), using the same formula, and continue… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm This generalizes to: C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 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 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm So binary search is an O(log(n)) algorithm. C++ for Everyone by Cay Horstmann Copyright © 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 first and then use a binary search? Sorting takes time, too! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Analyzing the Merge Sort Algorithm Is it worth the time to sort? It depends. C++ for Everyone by Cay Horstmann Copyright © 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 an O(n log(n)) sort plus an O(log(n)) binary search. But if one must make a number of searches in the same sequence, then sorting before the searches is definitely worthwhile. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Chapter Summary C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • Chapter Summary C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
    • End Chapter Twelve C++ for Everyone by Cay HorstmannSlides by Evan Gallagher Copyright © 2012 by John Wiley & Sons. All rights reserved