C++ Programming: From Problem Analysis To Program Design, Fifth Edition
C++ Programming: From Problem Analysis To Program Design, Fifth Edition
C++ Programming: From Problem Analysis To Program Design, Fifth Edition
Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type
Objectives
In this chapter, you will: Explore how to sort an array using the bubble sort, selection sort, and insertion sort algorithms Learn how to implement the binary search algorithm Become familiar with the vector type
List Processing
List: a collection of values of the same type Basic list operations:
Search the list for a given item Sort the list Insert an item in the list Delete an item from the list Print the list
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
3
Searching
Sequential search algorithm
Searching (cont'd.)
List with 1000 elements
Search item is the second item
Sequential search makes two key comparisons
Searching (cont'd.)
Sequential search
Not very efficient for large lists On average, number of key comparisons equal to half the size of the list Does not assume that the list is sorted
Bubble Sort
list[0]...list[n - 1]
List of n elements, indexed 0 to n - 1
Smaller elements move toward the top Larger elements move toward the bottom
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
8
10
11
12
13
n = 1000
500,000 key comparisons 250,000 item assignments
14
Selection Sort
Rearrange the list by selecting an element in the list and moving it to its proper position Finds the location of the smallest element in the unsorted portion of the list
Moves it to the top of the unsorted portion of the list
15
16
17
18
19
n = 1000
500,000 key comparisons 3000 item assignments
20
Insertion Sort
Sorts the list by moving each element to its proper place
21
22
23
24
25
26
n = 1000
250,000 key comparisons 250,000 item assignments
27
Binary Search
Much faster than a sequential search List must be sorted Divide and conquer Compare search item with middle element
Less than middle: search only upper half of list More than middle: search only lower half of list
28
29
30
31
L has1048576 elements
Binary search makes 42 item comparisons at most
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
32
33
35
36
37
38
Desired output:
40
42
43
Use a two-dimensional array to hold the next four columns of the output One-dimensional array to hold the total votes received by each candidate
44
45
47
48
49
Find the column in the array votesByRegion that corresponds to the regionNumber
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
50
51
52
53
54
55
If the input file does not exist, exit the program Initialize the arrays votesByRegion and totalVotes
Process the voting data and store the results in the array votesByRegion
58
59
Summary
List
Set of elements of the same type
Sorting algorithms
Bubble sort Selection sort Insertion sort
Binary search
Compare performance to sequential search
vector type
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
60