Chapter 19

advertisement
C++ Programming: Program Design Including Data Structures, Fourth Edition
Chapter 19
Search and Sorting Algorithms
At a Glance
Instructor’s Manual Table of Contents

Overview

Objectives

Teaching Tips

Quick Quizzes

Class Discussion Topics

Additional Projects

Additional Resources

Key Terms
19-1
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-2
Lecture Notes
Overview
Chapter 19 discusses searching and sorting algorithms in detail. Students will become
familiar with implementing searching and sorting algorithms and with analyzing the
performance of each algorithm. They will learn about the best-case, average-case, and
worst-case performance scenarios and be introduced to Big-O notation.
Chapter Objectives







Learn the various search algorithms
Explore how to implement the sequential and binary search algorithms
Discover how the sequential and binary search algorithms perform
Become aware of the lower bound on comparison-based search algorithms
Learn the various sorting algorithms
Explore how to implement the bubble, selection, insertion, quick, and merge sorting
algorithms
Discover how the sorting algorithms discussed in this chapter perform
Teaching Tips
Searching and Sorting Algorithms
1. Discuss the most important operation that can be performed on a list (the search
operation). Explain that some search algorithms only work on sorted data.
2. Discuss the types of data structures that will be used when implementing the search and
sort algorithms. Note that the header file for these algorithms is included at the end of
the chapter.
Search Algorithms
1. Discuss the rationale for analyzing search algorithms.
2. Define the key of a data set and discuss how it is used in search algorithms.
3. Discuss why the number of key comparisons is important when analyzing the
performance of search algorithms.
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-3
Sequential Search
1. Remind students that the sequential search (also called a linear search) works the same
for both array-based and linked lists. In other words, the search always starts at the first
element in the list and continues until either the item is found or the entire list is
searched.
2. Discuss why the worst case and the average case are two important aspects of an
algorithm’s performance.
3. Using the code in the section, illustrate how to analyze the best case, worst case, and
average case of the sequential search algorithm by counting the number of key
comparisons.
4. Discuss the efficiency of the sequential search algorithm on large data sets.
Binary Search
1. Discuss the ‘‘divide and conquer’’ technique used in the binary search and why the
binary search can be performed only on sorted lists. Use the figures and code in this
section to illustrate.
Performance of Binary Search
1. Discuss the performance of the binary search algorithm.
2. Compare the number of key comparisons for the binary search algorithm with the
number of key comparisons for the sequential search algorithm using the example in
this section.
3. Develop the formula for estimating the number of key comparisons for the binary
search algorithm.
Binary Search Algorithm and the class orderedArrayListType
1. Discuss how to add the binary search algorithm to the class
orderedArrayListType using the example in this section.
Quick Quiz 1
1. Define a key comparison.
Answer: A key comparison is a comparison between the key of the search item and the
key of an item in the list.
2. True or False: The sequential search algorithm requires that the list be sorted.
Answer: False
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-4
3. True or False: A binary search is more efficient than the sequential search.
Answer: True
Asymptotic Notation: Big-O Notation
1. Using Examples 19-3 and 19-4, discuss counting the number of operations in a code
segment and demonstrate how to find the dominant operation.
2. Discuss the relative contribution of the dominant operation(s) to the overall
performance of an algorithm.
Teaching
Tip
Ask your students what criteria they would use to identify dominant operations.
For example, under what circumstances would a read operation be considered a
dominant operation?
Teaching
Tip
Remind students that it is important to think about an algorithm’s performance
on large data sets.
Teaching
Tip
Ask your students to what degree operations that are only performed once impact
overall algorithm performance.
Teaching
Tip
Have your students think about when it might be important to consider the
operations performed within a function instead of simply considering the call to
that function.
3. Using Tables 19-4 and 19-5, and Figure 19-9, discuss the growth rate and execution
time of various functions, reminding students that the performance for the sequential
search algorithm is represented by the columns for f(n) = n, and the performance for the
binary search algorithm is represented by the columns for f(n) = log2n.
4. Define the term “asymptotic.”
5. Using Table 19-6, demonstrate how to identify the dominant term in a function.
6. Define what it means to say that f(n) is Big-O of g(n). Use Examples 19-5 through 19-7
to develop O(g(n)) for constant, linear, and quadratic growth functions and to lay the
foundation for the theorem on page 1180.
C++ Programming: Program Design Including Data Structures, Fourth Edition
Teaching
Tip
19-5
Ask your students to think about what it means to say an algorithm is O(1).
Lower Bound on Comparison-Based Search Algorithms
1. Define the term “comparison-based search algorithm.”
2. Discuss the lower bound on search algorithms that are comparison-based.
Quick Quiz 2
1. Define asymptotic.
Answer: Aymptotic refers to the study of a function of n as n becomes larger and larger
without bound.
2. What types of operations are typically dominant operations in search algorithms?
Answer: key comparisons
3. True or False: A function that is O(n) is more efficient than a function that is O(log2n).
Answer: False
Sorting Algorithms
1. Introduce the sorting algorithms that will be covered in the remainder of this chapter,
the types of lists to which those algorithms can be applied (array-based lists or linked
lists), and how the performance of the sorting algorithms will be analyzed.
Sorting a List: Bubble Sort
1. Using Figures 19-10 though 19-14, discuss the algorithm for the bubble sort.
Teaching
Tip
Ask your students how they would modify the C++ function that implements the
bubble sort algorithm (given on page 1185) to sort a list in descending order.
Analysis: Bubble Sort
1. Discuss the two types of operations that are important to consider when analyzing a sort
algorithm (key comparisons and data movements).
2. Discuss why the number of comparisons and the number of assignments for the bubble
sort algorithm are each O(n2).
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-6
Bubble Sort Algorithm and the class unorderedArrayListType
1. Discuss how to use the bubble sort algorithm in the class
unorderedArrayListType.
Selection Sort: Array-Based Lists
1. Discuss the selection sort algorithm using Figures 19-15 through 19-21.
Analysis: Selection Sort
1. Discuss the performance of the selection sort in terms of the assignment statements in
the function swap and the key comparisons in the function minLocation.
Teaching
Tip
Ask your students to think about what the best case and worst case would be for
the selection sort.
Insertion Sort: Array-Based Lists
1. Discuss the insertion sort algorithm using the pseudocode algorithm on page 1196 and
Figures 19-29 through 19-35.
Analysis: Insertion Sort
1. Discuss the performance of the insertion sort.
2. Using Table 19-9, compare the average case behavior of each sort algorithm discussed
thus far (bubble sort, selection sort, and insertion sort).
Teaching
Tip
Discuss the relative contribution of key comparisons and assignment statements
to Big-O for insertion sort.
Lower Bound on Comparison-Based Sort Algorithms
1. Discuss the graph called a comparison tree, defining the terms “node,” “root,” “leaf,”
“branch,” “path,” and “binary tree.”
2. Using Figure 19-36, illustrate the comparison tree for a list containing three items.
3. Using the comparison tree, discuss the worst-case performance of any comparisonbased sorting algorithm, illustrating that the worst case is at least O(nlog2n).
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-7
Quick Sort: Array-Based Lists
1. Define the term “pivot” and discuss how a pivot element is typically chosen for the
quick sort algorithm.
2. Discuss the divide-and-conquer approach used in the quick sort algorithm using Figures
19-37 through 19-47.
Analysis: Quick Sort
1. Use Table 19-10 to discuss the worst-case number of comparisons for the quick sort
algorithm.
Teaching
Tip
Ask your students to think about what the list looks like in the worst-case
scenario for the quick sort.
Merge Sort: Linked List-Based Lists
1. Note that merge sort is a sorting algorithm whose behavior is always O(nlog2n).
2. Use Figure 19-48 to illustrate the merge sort process. Briefly describe the three-step
divide-and-conquer approach of merge sort.
Divide
1. Using the figures in this section, discuss the dividing process of the merge sort
algorithm.
Merge
1. Using the figures in this section, discuss the merging process of the merge sort
algorithm.
Teaching
Tip
Discuss the amount of overhead related to the merge sort and how the overhead
is taken into account in the Big-O for merge sort.
Analysis: Merge Sort
1. Discuss where the actual work (assignments and comparisons) is done in the merge sort.
2. Using Figure 19-56, discuss the relationship between the recursion level, the number of
calls to the merge function, and the number of elements related to each call.
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-8
3. Step through the “Election Results” Programming Example to consolidate the concepts
presented in this chapter. Encourage students to compile and run this program on their
own.
Quick Quiz 3
1. For a comparison tree, how many leaf nodes are there for a list containing five items? In
other words, how many permutations are there?
Answer: 5! = 3125
2. True or False: Both the quick sort and the merge sort algorithms sort a list by
partitioning it.
Answer: True
3. The merge sort algorithm partitions the list by ____________________.
Answer: dividing it in the middle
Class Discussion Topics
1. Ask your students to think about algorithms in specific application domains for which
performance is especially critical, such as computationally-intensive scientific
computing and the management of Census Bureau databases. Given the functions
graphed in Figure 19-9 and the information provided in Table 19-7, would it be
acceptable to use a linear algorithm in these application domains? Why or why not?
2. Discuss any assumptions that have been made related to duplicate items in a list. How
do (or would) the searching and sorting algorithms presented in this chapter handle
duplicates?
3. Ask the students to think about writing an algorithm for a bin sort. The algorithm reads
in a deck of cards, one card at a time, and writes out the deck of cards in sorted order
(Two of Spades to Ace of Spades, Two of Clubs to Ace of Clubs, Two of Hearts to Ace
of Hearts, Two of Diamonds to Ace of Diamonds). What assumptions can be made
about the input file in this context? How do those assumptions impact the design of the
algorithm? For example, does the algorithm require any key comparisons? What is the
performance of the algorithm?
Additional Projects
1. Write a program to read first and last names from an input file and print the names in
alphabetical order by last name. Students can use a two-dimensional array or parallel
arrays. Output should have one name per line in table format. The table should have a
header row containing the labels “Last Name” and “First Name.”
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-9
2. Define a “stable sort” to be a sort algorithm for which duplicate values are in the same
order after the sort as they were before the list was sorted. Have your students choose a
sorting algorithm and write a program to determine if the algorithm is stable.
Additional Resources
1. Big O Notation:
http://en.wikipedia.org/wiki/Big_O_notation
2. Searching:
http://cprogramming.com/discussionarticles/sorting_and_searching.html
3. Sorting:
www.cprogramming.com/tutorial/computersciencetheory/sorting2.html
www.cprogramming.com/tutorial/computersciencetheory/quicksort.html
www.cprogramming.com/tutorial/computersciencetheory/radix.html
http://en.wikipedia.org/wiki/Bin_sort
Key Terms
 asymptotic: the study of a function of n as n becomes larger and larger without bound
 Big-O: a function f(n) is Big-O of g(n) if there exists positive constants c and n0 such
that f(n) <= cg(n) for all n >= n0
 binary search: a very fast search algorithm that uses the divide-and-conquer technique
and can only be performed on sorted lists
 binary tree: a comparison tree in which each comparison has two outcomes
 branch: a straight line in a graph that connects two nodes
 bubble sort: a sort algorithm that sorts a list by comparing successive pairs of elements
in the list and swapping those elements if they are out of order
 comparison–based search algorithm: a search algorithm that searches a list by
comparing the target element with the list elements
 comparison tree: a graph that can be used to trace the execution of a comparison-based
sort algorithm
 heap sort: a sorting algorithm
 insertion sort: a sort algorithm that sorts a list by moving each element to its proper
place in the sorted portion of the list
 key: a special member of each item in a data set that uniquely identifies the item in the
data set
 leaf: a node that represents the final ordering of the nodes, drawn as a rectangle
 merge sort: a sort algorithm that sorts a list using a divide-and-conquer technique in
which a list is repeatedly divided into two sublists of nearly equal size until each sublist
is sorted; the sublists are then merged together
 node: each comparison in a comparison tree, drawn as a circle
 path: a path from one node, x, to another node, y, is a sequence of branches from x to y
 pivot: an element of an unsorted list that is used to divide an unsorted list into two
sublists during the quick sort algorithm
C++ Programming: Program Design Including Data Structures, Fourth Edition
19-10
 quick sort: a sort algorithm that sorts a list using a divide and conquer technique to
partition a list into a lower sublist (which contains items less than the pivot) and an
upper sublist (which contains items greater than the pivot); each sublist is sorted and
combined into one list in such a way so that the combined list is sorted
 root: the top node in a comparison tree
 selection sort: a sort algorithm that sorts a list by selecting the smallest element in the
unsorted portion of the list and moving that element to the top of the unsorted list
 sequential search: a linear search algorithm that can be performed on both array-based
and linked lists, and starts at the first element in the list and continues until either the
item is found in the list or the entire list is searched
 target: the search item
Download