Professional Documents
Culture Documents
Fifth Edition: Sorting and Searching Arrays
Fifth Edition: Sorting and Searching Arrays
Design
Fifth Edition
Chapter 9
Sorting and Searching
Arrays
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.1 The Bubble Sort Algorithm (1 of 3)
Bubble sort is a simple sorting algorithm for rearranging the
contents of an array
– Useful for alphabetical lists and numerical sorting
– Can be done in ascending or descending order
– With the Bubble Sort, array elements are compared and
numbers bubble toward the end of the array (assuming your
sorting in ascending order)
– Swapping elements must be done in order to properly sort
the array
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.1 The Bubble Sort Algorithm (2 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.1 The Bubble Sort Algorithm (3 of 3)
Inside Program 9-1
– maxElement variable holds the subscript of the last
element that is to be compared to its neighbor
– index variable is an array subscript in one loop
– The outer loop iterates for each element in the array
– The inner loop iterates for each of the unsorted array
elements
– The if statement does the comparison
Sorting an array of strings to put information in alphabetical
order can be done with a Bubble Sort
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.2 The Selection Sort Algorithm (1 of 3)
The selection sort works similar to the bubble sort, but more
efficient
– Bubble sort moves one element at a time
– Selection sort performs fewer swaps because it moves
items immediately to their final position
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.2 The Selection Sort Algorithm (2 of 3)
Figure 9-17 Flowchart for the selectionSort module
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.2 The Selection Sort Algorithm (3 of 3)
Inside Figure 9-17
– minIndex holds the subscript of the element with the
smallest value
– minValue holds the smallest value found
– The outer loop iterates for each element in the array, except
the last
– The inner loop performs the scan
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.3 The Insertion Sort Algorithm (1 of 3)
The insertion sort algorithm sorts the first two elements, which
becomes the sorted part of the array
– Each remaining element is then inserted into the sorted part
of the array at the correct location
– Also more efficient than the bubble sort
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.3 The Insertion Sort Algorithm (2 of 3)
Figure 9-24 Flowchart for the insertionSort module
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.3 The Insertion Sort Algorithm (3 of 3)
Inside Figure 9-24
– scan is used to scan through the array
– unsortedValue holds the first unsorted value
– The outer loop steps the index variable through each
subscript, starting at 1
– The inner loop moves the first element outside the sorted
subset and into its proper position
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
The Swap Module
A closer look at the swap module
– In most of the sorts, a swap module can be called
– It is the same in each sorting algorithm and only changes in
the parameter list to account for the type of data passed to
it
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.4 The Binary Search Algorithm (1 of 4)
The binary search algorithm locates an item in an array by
repeatedly dividing the array in half
– Each time it divides the array, it eliminates the half of the
array that does not contain the item
– It’s more sequential than the selection search because each
time it cuts the array in half and makes a smaller number of
comparisons to find a match
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.4 The Binary Search Algorithm (2 of 4)
How it works
– Requires that the array is first sorted
– The first comparison is done with the middle element of
the array to see if it is greater than or less than the number
that is being searched
– If it’s greater than, then the number must be in the first half
of the array
– If it’s less than, then the number must be in the second half
of the array
– This process is continued until the match if found
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.4 The Binary Search Algorithm (3 of 4)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.4 The Binary Search Algorithm (4 of 4)
Inside Program 9-17
– middle is used to store the calculated middle index
– value stores the value being searched for
– A loop hold the search and iterates as long as there are
elements or until a match is found
– Nested if-then-else’s perform the comparisons
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
9.5 Focus on Languages: Java
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
public static void bubbleSort(int[] array)
{
int maxElement; // Marks the last element to compare
int index; // Index of an element to compare
int temp; // Used to swap to elements
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
def bubble_sort(arr):
# Set max_element to the length of the arr list, minus
# one. This is necessary for the outer loop.
max_element = len(arr) - 1
# The outer loop positions max_element at the last element
# to compare during each pass through the list. Initially
# max_element is the index of the last element in the array.
# During each iteration, it is decreased by one.
while max_element >= 0:
# Set index to 0, necessary for the inner loop.
index = 0
# The inner loop steps through the list, comparing
# each element with its neighbor. All of the elements
# from index 0 thrugh max_element are involved in the
# comparison. If two elements are out of order, they
# are swapped.
while index <= max_element - 1:
# Compare an element with its neighbor.
if arr[index] > arr[index + 1]:
# Swap the two elements.
temp = arr[index]
arr[index] = arr[index + 1]
arr[index + 1] = temp
# Increment index.
index = index + 1
# Decrement max_element.
max_element = max_element - 1
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
def selection_sort(arr):
# Set start_scan to 0. This is necessary for
# the outer loop. It is the starting position
# of the scan.
start_scan = 0
# The outer loop iterates once for each element in the
# list. The start_scan variable marks the position where
# the scan should begin.
while start_scan < len(arr) - 1:
# Assume the first element in the scannable area
# is the smallest value.
min_index = start_scan
min_value = arr[start_scan]
# Initialize index for the inner loop.
index = start_scan + 1
# Scan the list, starting at the 2nd element in
# the scannable area. We are looking for the smallest
# value in the scannable area.
while index < len(arr):
if arr[index] < min_value:
min_value = arr[index]
min_index = index
# Increment index.
index = index + 1
# Swap the element with the smallest value
# with the first element in the scannable area.
arr[min_index] = arr[start_scan]
arr[start_scan] = min_value
# Increment start_scan.
start_scan = start_scan + 1
Python Selection Sort
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
def insertion_sort(arr):
# Set index to 1 for the outer loop.
index = 1
# Increment index.
index = index + 1
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
def binary_search(arr, value):
# Set the initial values.
first = 0
last = len(arr) - 1
position = -1
found = False
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
void bubbleSort(int array[], int size)
{
int maxElement; // Marks the last element to compare
int index; // Index of an element to compare
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
void selectionSort(int array[], int size)
{
int startScan; // Starting position of the scan
int index; // To hold a subscript value
int minIndex; // Element with smallest value in the scan
int minValue; // The smallest value found in the scan
// The outer loop iterates once for each element in the
// array. The startScan variable marks the position where
// the scan should begin.
for (startScan = 0; startScan < (size-1); startScan++)
{
// Assume the first element in the scannable area
// is the smallest value.
minIndex = startScan;
minValue = array[startScan];
}
b = temp;
C++ Selection Sort
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
void insertionSort(int array[], int size)
{
int unsortedValue; // The first unsorted value
int scan; // Used to scan the array
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
int binarySearch(string array[], string value, int size)
{
int first; // First array element
int last; // Last array element
int middle; // Midpoint of search
int position; // Position of search value
bool found; // Flag
// Set the inital values.
first = 0;
last = size - 1;
position = -1;
found = false;
// Search for the value.
while (!found && first <= last)
{
// Calculate midpoint
middle = (first + last) / 2;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved