Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

Java Programming: From Problem

Analysis to Program Design, 5e

Chapter 14
Searching and Sorting
Chapter Objectives
• Learn how to implement the sequential
search algorithm
• Explore how to sort an array using selection
sort and insertion sort algorithms
• Learn how to implement the binary search
algorithm

Java Programming: From Problem Analysis to Program Design, 5e 2


List Processing
• List: a set of values of the same type
• Basic operations performed on a list
– Search list for given item
– Sort list
– Insert item in list
– Delete item from list

Java Programming: From Problem Analysis to Program Design, 5e 3


Search
• Necessary components to search a list
– Array containing the list
– Length of the list
– Item for which you are searching

• Sequential search is discussed in Chapter 9

Java Programming: From Problem Analysis to Program Design, 5e 4


Search (continued)

• If the search item is the second item in the list, the


sequential search makes two key comparisons (also called
item comparisons) to determine whether the search item is
in the list
• If the search item is the 900th item in the list, the
sequential search makes 900 key comparisons to determine
whether the search item is in the list
• If the search item is not in the list, the sequential search
makes 1000 key comparisons
Java Programming: From Problem Analysis to Program Design, 5e 5
Search (continued)
• If searchItem is always at the end of the list, it will take
many comparisons to find searchItem
• If searchItem is not in the list, then we will compare
searchItem with every element in the list
• A sequential search is therefore not efficient for large lists; in
fact, it can be proved that, on average, the number of
comparisons made by a sequential search is equal to half the
size of the list

Java Programming: From Problem Analysis to Program Design, 5e 6


Selection Sort

• List is sorted by selecting list element and


moving it to its proper position
• Algorithm finds position of smallest
element and moves it to top of unsorted
portion of list
• Repeats process above until entire list is
sorted

Java Programming: From Problem Analysis to Program Design, 5e 7


Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 5e 8


Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 5e 9


Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 5e 10


Selection Sort (continued)
public static void selectionSort(int[] list,
int listLength)
{
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < listLength – 1; index++)
{
smallestIndex = index;
for (minIndex = index + 1;
minIndex < listLength; minIndex++)
if (list[minIndex] < list[smallestIndex])
smallestIndex = minIndex;
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}
Java Programming: From Problem Analysis to Program Design, 5e 11
Selection Sort (continued)
• It is known that for a list of length n, selection
sort makes exactly n(n – 1) / 2 key comparisons
and exactly 3(n – 1) item assignments
• Therefore, if n = 1000, then to sort the list,
selection sort makes 500,000 key comparisons
and about 3000 item assignments

Java Programming: From Problem Analysis to Program Design, 5e 12


Insertion Sort
• The insertion sort algorithm sorts the list by
moving each element to its proper place

Java Programming: From Problem Analysis to Program Design, 5e 13


Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 5e 14


Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 5e 15


Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 5e 16


Insertion Sort (continued)
public static void insertionSort(int[] list,
int listLength)
{
int firstOutOfOrder, location;
int temp;
for (firstOutOfOrder = 1; firstOutOfOrder < listLength;
firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
{
temp = list[firstOutOfOrder];
location = firstOutOfOrder;
do
{
list[location] = list[location - 1];
location--;
}
while(location > 0 && list[location - 1] > temp);
list[location] = temp;
}
} //end insertionSort

Java Programming: From Problem Analysis to Program Design, 5e 17


Insertion Sort (continued)

• It is known that for a list of length n, on average,


the insertion sort makes (n2 + 3n – 4) / 4 key
comparisons and about n(n – 1) / 4 item
assignments
• Therefore, if n = 1000, then to sort the list, the
insertion sort makes about 250,000 key
comparisons and about 250,000 item assignments

Java Programming: From Problem Analysis to Program Design, 5e 18


Binary Search Algorithm
• Search item is compared with middle
element of list
• If search item < middle element of list,
search is restricted to first half of the list
• If search item > middle element of list,
search second half of the list
• If search item = middle element, search is
complete
Java Programming: From Problem Analysis to Program Design, 5e 19
Binary Search Algorithm (continued)
• Determine whether 75 is in the list

Java Programming: From Problem Analysis to Program Design, 5e 20


Binary Search Algorithm
(continued)

Java Programming: From Problem Analysis to Program Design, 5e 21


public static int binarySearch(int[] list, int listLength,
int searchItem)
{
int first = 0;
int last = listLength - 1;
int mid;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid] == searchItem)
found = true;
else if (list[mid] > searchItem)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return –1;
} //end binarySearch
Java Programming: From Problem Analysis to Program Design, 5e 22
Binary Search Algorithm (continued)

Java Programming: From Problem Analysis to Program Design, 5e 23


Binary Search Algorithm (continued)

Java Programming: From Problem Analysis to Program Design, 5e 24


Performance of the Binary Search

• Suppose that L is a list of size 1048576


• Because 1048576 = 220, it follows that the
while loop in binary search will have at most
21 iterations to determine whether an element is
in L
• Every iteration of the while loop makes two
key (that is, item) comparisons

Java Programming: From Problem Analysis to Program Design, 5e 25


Performance of the Binary Search
(continued)
• To determine whether an element is in L, binary
search makes at most 42 item comparisons
– On the other hand, on average, a sequential search will
make 524,288 key (item) comparisons to determine
whether an element is in L
• In general, if L is a sorted list of size n, to
determine whether an element is in L, the binary
search makes at most 2log2n + 2 key (item)
comparisons
Java Programming: From Problem Analysis to Program Design, 5e 26
Programming Example:
Election Results
• Input: two files
– File 1: candidates’ names
– File 2: voting data
• Voting Data Format
– candidate_name region# number_of_votes_for_this_candidate

Java Programming: From Problem Analysis to Program Design, 5e 27


Programming Example:
Election Results (continued)
• Output: election results in a tabular form
– Each candidate’s name
– Number of votes each candidate received in
each region
– Total number of votes each candidate received

Java Programming: From Problem Analysis to Program Design, 5e 28


Programming Example:
Election Results (Solution)
• The solution includes:
– Reading the candidates’ names into the array
candidateName
– A two-dimensional array consisting of the votes
by region
– An array consisting of the total votes

Java Programming: From Problem Analysis to Program Design, 5e 29


Programming Example:
Election Results (Solution) (continued)
• The solution includes (continued):
– Sorting the array candidateName
– Processing the voting data
– Calculating the total votes received by each
candidate
– Outputting the results in tabular form

Java Programming: From Problem Analysis to Program Design, 5e 30


Programming Example:
Election Results

Java Programming: From Problem Analysis to Program Design, 5e 31


Programming Example:
Election Results (continued)

Java Programming: From Problem Analysis to Program Design, 5e 32


Chapter Summary
• Lists
• Searching lists
– Sequential searching
– Binary search
• Sorting lists
– Selection sort
– Insertion sort

Java Programming: From Problem Analysis to Program Design, 5e 33

You might also like