View
219
Download
1
Category
Preview:
Citation preview
Chapter 5
Searching and Sorting
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-2
Chapter Objectives
• Examine the linear search and binary search algorithms
• Examine several sorting algorithms, including:• selection sort• insertion sort• bubble sort• quick sort• merge sort
• Discuss the complexity of these algorithms
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-3
Searching
• Searching is the process of finding a target element among a group of items (the search pool), or determining that it isn't there
• This requires repetitively comparing the target to candidates in the search pool
• An efficient sort performs no more comparisons than it has to
• The size of the search pool is a factor
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-4
The Comparable Interface• We want to define the algorithms such that
they can search any set of objects• Therefore we will search objects that
implement the Comparable interface• It contains one method, compareTo, which is
designed to return an integer that specifies the relationship between two objects:
obj1.compareTo(obj2)
• This call returns a number less than, equal to, or greater than 0 if obj1 is less than, equal to, or greater than obj2, respectively
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-5
Linear Search
• A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted
• This approach does not assume the items in the search pool are in any particular order
• We just need to be able to examine each element in turn (in a linear fashion)
• It's fairly easy to understand, but not very efficient
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-6
figure 5.1 A linear search
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-7
Linear Search//-----------------------------------------------------------------
// Searches the specified array of objects for the target value
// using a linear search algorithm.
//-----------------------------------------------------------------
public static boolean linearSearch (Comparable[] data,
Comparable target)
{
int index = 0;
boolean found = false;
while (!found && index < data.length)
{
if (data[index].compareTo(target) == 0)
found = true;
index++;
}
return found;
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-8
Binary Search
• If the search pool is sorted, then we can be more efficient than a linear search
• A binary search eliminates large parts of the search pool with each comparison
• Instead of starting the search at one end, we begin in the middle
• If the target isn't found, we know that if it is in the pool at all, it is in one half or the other
• We can then jump to the middle of that half, and continue similarly
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-9
figure 5.2 A binary search
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-10
Binary Search
• For example, find the number 29 in the following sorted list of numbers:
8 15 22 29 36 54 55 61 70 73 88
• Compare the target to the middle value 54• We now know that if 29 is in the list, it is in the
front half of the list• With one comparison, we've eliminated half of
the data• Then compare to 22, eliminating another
quarter of the data, etc.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-11
Binary Search
• A binary search algorithm is often implemented recursively
• Each recursive call searches a smaller portion of the search pool
• The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool
• At any point there may be two "middle" values, in which case either could be used
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-12
Binary Search//-----------------------------------------------------------------
// Searches the specified array of objects for the target value
// using a binary search algorithm.
//-----------------------------------------------------------------
public static boolean binarySearch (Comparable[] data, int min,
int max, Comparable target)
{
boolean found = false;
int midpoint = (min + max) / 2; // determine the midpoint
if (data[midpoint].compareTo(target) == 0)
found = true;
else
if (data[midpoint].compareTo(target) > 0)
if (min <= midpoint-1)
found = binarySearch(data, min, midpoint-1, target);
else
if (midpoint+1 <= max)
found = binarySearch(data, midpoint+1, max, target);
return found;
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-13
Comparing Search Algorithms
• On average, a linear search would examine n/2 elements before finding the target
• Therefore, a linear search is O(n)• The expected case for a binary search is
(log2n)/2 comparisons
• A binary search is a logarithmic algorithm
• It has a time complexity of O(log2n)
• But keep in mind that the search pool must be sorted
• For large n, a binary search is much faster
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-14
Sorting
• Sorting is the process of arranging a group of items into a defined order based on particular criteria
• Many sorting algorithms have been designed• Sequential sorts require approximately n2
comparisons to sort n elements
• Logarithmic sorts typically require nlog2n comparisons to sort n elements
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-15
Sorting
• Let's define a generic sorting problem that any of our sorting algorithms could help solve
• As with searching, we must be able to compare one element to another
• See SortPhoneList.java (page 126)• See Contact.java (page 128)
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-16
Selection Sort
• Selection sort orders a list of values by repetitively putting a particular value into its final position
• More specifically:• find the smallest value in the list• switch it with the value in the first position• find the next smallest value in the list• switch it with the value in the second position• repeat until all values are in their proper places
figure 5.3 Illustration of selection sort processing
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-18
Selection Sort//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] data)
{
int min;
Comparable temp;
for (int index = 0; index < data.length-1; index++)
{
min = index;
for (int scan = index+1; scan < data.length; scan++)
if (data[scan].compareTo(data[min]) < 0)
min = scan;
// Swap the values
temp = data[min];
data[min] = data[index];
data[index] = temp;
}
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-19
Insertion Sort• Insertion sort orders a list of values by
repetitively inserting a particular value into a sorted subset of the list
• More specifically:• consider the first item to be a sorted sublist of
length 1• insert the second item into the sorted sublist,
shifting the first item if needed• insert the third item into the sorted sublist, shifting
the other items as needed• repeat until all values have been inserted into their
proper positions
figure 5.4 Illustration of insertion sort processing
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-21
Insertion Sort//-----------------------------------------------------------------
// Sorts the specified array of objects using an insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] data)
{
for (int index = 1; index < data.length; index++)
{
Comparable key = data[index];
int position = index;
// Shift larger values to the right
while (position > 0 && data[position-1].compareTo(key) > 0)
{
data[position] = data[position-1];
position--;
}
data[position] = key;
}
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-22
Bubble Sort
• Bubble sort orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary
• More specifically:• scan the list, exchanging adjacent elements if they
are not in relative order; this bubbles the highest value to the top
• scan the list again, bubbling up the second highest value
• repeat until all elements have been placed in their proper order
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-23
Bubble Sort//-----------------------------------------------------------------
// Sorts the specified array of objects using a bubble sort
// algorithm.
//-----------------------------------------------------------------
public static void bubbleSort (Comparable[] data)
{
int position, scan;
Comparable temp;
for (position = data.length-1; position >= 0; position--)
{
for (scan = 0; scan < position-1; scan++)
{
if (data[scan].compareTo(data[scan+1]) > 0)
{
// Swap the values
temp = data[scan];
data[scan] = data[scan + 1];
data[scan + 1] = temp;
}
}
}
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-24
Comparing Sorts
• We've seen three sorts so far:• selection sort• insertion sort• bubble sort
• They all use nested loops and perform approximately n2 comparisons
• They are relatively inefficient• Now we will turn our attention to more
efficient sorts
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-25
Quick Sort
• Quick sort orders a list of values by partitioning the list around one element, then sorting each partition
• More specifically:• choose one element in the list to be the partition
element• organize the elements so that all elements less
than the partition element are to the left and all greater are to the right
• apply the quick sort algorithm (recursively) to both partitions
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-26
Quick Sort
• The choice of the partition element is arbitrary• For efficiency, it would be nice if the partition
element divided the list roughly in half• The algorithm will work in any case, however• We will divide the work into two methods:• quickSort – performs the recursive algorithm• findPartition – rearranges the elements into
two partitions
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-27
Quick Sort//-----------------------------------------------------------------
// Sorts the specified array of objects using the quick sort
// algorithm.
//-----------------------------------------------------------------
public static void quickSort (Comparable[] data, int min, int max)
{
int indexOfPartition;
if (max-min > 0)
{
// Create partitions
indexOfPartition = findPartition (data, min, max);
// Sort the left side
quickSort (data, min, indexOfPartition-1);
// Sort the right side
quickSort (data, indexOfPartition+1, max);
}
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-28
The findPartition method//-----------------------------------------------------------------
// Rearranges the elements in the sort area into two partitions.
//-----------------------------------------------------------------
private static int findPartition (Comparable[] data, int min, int max)
{
int left, right;
Comparable temp, partitionelement;
// Use the first element as the partition element
partitionelement = data[min];
left = min;
right = max;
while (left < right)
{
// search for an element that is > the partition element
while (data[left].compareTo(partitionelement) <= 0 &&
left < right)
left++;
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-29
findPartition continued // search for an element that is < the partition element
while (data[right].compareTo(partitionelement) > 0)
right--;
// swap the elements
if (left < right)
{
temp = data[left];
data[left] = data[right];
data[right] = temp;
}
}
// Move partition element to partition index
temp = data[min];
data[min] = data[right];
data[right] = temp;
return right;
}
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-30
Merge Sort
• Merge sort orders a list of values by recursively dividing the list in half until each sublist has one element, then recombining
• More specifically:• divide the list into two roughly equal parts• recursively divide each part in half, continuing until
a part contains only one element• merge the two parts into one sorted list• continue to merge parts as the recursion unfolds
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-31
figure 5.5 The decomposition of merge sort
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-32
figure 5.6 The merge portion of the merge sort algorithm
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-33
Merge Sort//-----------------------------------------------------------------
// Sorts the specified array of objects using the merge sort
// algorithm.
//-----------------------------------------------------------------
public static void mergeSort (Comparable[] data, int min, int max)
{
Comparable temp[];
int index1, left, right;
// Return on list of length one
if (min == max)
return;
// Find the length and the midpoint of the list
int size = max – min + 1;
int pivot = (min + max) / 2;
temp = new Comparable[size];
mergeSort (data, min, pivot); // Sort left half of list
mergeSort (data, pivot+1, max); // Sort right half of list
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1-34
Merge Sort continued // Copy sorted data into workspace
for (index1 = 0; index1 < size; index1++)
temp[index1] = data[min + index1];
// Merge the two sorted lists
left = 0;
right = pivot – min + 1;
for (index1 = 0; index1 < size; index1++)
{
if (right <= max – min)
if (left <= pivot – min)
if (temp[left].compareTo(temp[right]) > 0)
data[index1 + min] = temp[right++];
else
data[index1 + min] = temp[left++];
else
data[index1 + min] = temp[right++];
else
data[index1 + min] = temp[left++];
}
}
Recommended