C++ Programming: From Problem Analysis to
Program Design, Fifth Edition
Chapter 10:
Applications of Arrays and the class
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
Searching
• Sequential search algorithm
Searching (cont'd.)
• List with 1000 elements
– Search item is the second item
• Sequential search makes two key comparisons
– Search item is the 900th item
• Sequential search makes 900 key comparisons
– Search item is not in the list
• Sequential search makes 1000 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
Bubble Sort (cont'd.)
• Series of
n - 1
iterations
– Successive elements
list[index]
and
list[index + 1]
of
list
are compared
– If
list[index]
>
list[index + 1]
• Elements
list[index]
and
list[index + 1]
are swapped
• Smaller elements move toward the top
• Larger elements move toward the bottom
Bubble Sort (cont'd.)
Bubble Sort (cont'd.)
Bubble Sort (cont'd.)
Bubble Sort (cont'd.)
Bubble Sort (cont'd.)
Bubble Sort (cont'd.)
• List of length
n
– Exactly
n
(
n -
1) / 2 key comparisons
– On average
n
(
n -
1) / 4 item assignments
• n
= 1000
– 500,000 key comparisons
– 250,000 item assignments
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
Selection Sort (cont'd.)
Selection Sort (cont'd.)
Selection Sort (cont'd.)
• In the unsorted portion of the list:
– Find the location of the smallest element
– Move the smallest element to the beginning of
the unsorted list
Selection Sort (cont'd.)
Selection Sort (cont'd.)
• List of length
n
– Exactly
n
(
n -
1
)
/ 2
key comparisons
– 3(
n -
1
)
item assignments
• n
= 1000
– 500,000 key comparisons
– 3000 item assignments
Insertion Sort
• Sorts the list by moving each element to
its proper place
Insertion Sort (cont'd.)
• Consider the element
list[4]
– First element of unsorted list
–
list[4] < list[3]
• Move
list[4]
to proper location
• At
list[2]
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24
Insertion Sort (cont'd.)
• During the sorting phase
– Array containing the list is divided into two
sublists: sorted and unsorted
Insertion Sort (cont'd.)
Insertion Sort (cont'd.)
• List of length
n
– About (
n
2
+
3
n –
4) / 4 key comparisons
– About
n
(
n –
1) / 4 item assignments
• n
= 1000
– 250,000 key comparisons
– 250,000 item assignments
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
Binary Search (cont'd.)
Binary Search (cont'd.)
Binary Search (cont'd.)
Performance of Binary Search
• L
is a sorted list of size 1024
– Every iteration of the
while
loop cuts the size
of the search list by half
– At most, 11 iterations to determine whether
x
is in
L
– Binary search will make 22 comparisons at
most
• L has1048576 elements
– Binary search makes 42 item comparisons at
most
Performance of Binary Search
(cont'd.)
• List of length
n
– Maximum number comparisons 2log
2
n
+ 2
vector type (class)
• Only a fixed number of elements can be
stored in an array
• Inserting and removing elements causes
shifting
•
vector
type implements a list
–
vector
container
–
vector
–
vector object
–
object
vector type (class) (cont'd.)
vector type (class) (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 37
vector type (class) (cont'd.)
Programming Example: Election
Results
• Presidential election for the student council of
your local university
• Write a program to analyze the data and
report the winner
• Four major divisions labeled as Region 1,
Region 2, Region 3, and Region 4
– Each division has several department
– Each department manages its own voting process
and directly reports the results to the election
committee
Programming Example: Election
Results (cont'd.)
• Desired output:
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 40