Chapter 18
Sorting and Searching
Chapter Goals
- To study the several searching and sorting algorithms
- To appreciate that algorithms for the same task can differ widely in performance
- To understand big-Oh notation
- To learn how to estimate and compare the performance of algorithms
- To learn how to measure the running time of a program
Sorting an Array of Integers
- The selection sort algorithm repeatedly finds the smallest element in the unsorted
tail region of the array and moves it to the front
- Array in original order
- Find the smallest and swap it with the first element
- Find the next smallest. It is already in the correct place
- Find the next smallest and swap it with the first element of unsorted portion
- Repeat
- When the unsorted portion is of length 1, we are done
- This algorithm is slow when run on large data sets
File SelectionSorter.java
File SelectionSortTest.java
File ArrayUtil.java
Profiling the Selection Sort Algorithm
- We want to measure the time the algorithm takes to execute
- Exclude the time the program takes to load
- Exclude output time
- Create a StopWatch class to measure execution time of an algorithm
- It can start, stop and give elapsed time
- Use System.currentTimeMillis
method
- Create a Stopwatch object
- Start the stopwatch just before the sort
- Stop the stopwatch just after the sort
- Read the elapsed time
File StopWatch.java
File SelectionSortTimer
Time Taken By Selection Sort on Various Size Arrays
Doubling the size of the array more than doubles the time needed
to sort it
Table
Graph 
Analyzing the Performance of Selection Sort Algorithm
- In an array of size n, count how many times an array element is visited
- To find the smallest, visit n elements + 2 visits for the swap
- To find the next smallest, visit (n-1) elements + 2 visits for
the swap
- The last term is 2 elements visited to find the smallest + 2 visits
for the swap
- n + 2 + (n-1) + 2 + (n-2) +2 + . . .+ 2 + 2
- This can be simplified to n2 /2 + n/2
- 3
- n/2 - 3 is small compared to n2 /2 - so let's
ignore it
- also ignore the 1/2 - it divides out when comparing ratios
- The number of visits is of the order n2
- Using big-Oh notation: The number of visits is O(n2)
- Multiplying the number of elements in an array by 2 multiplies
the processing time by 4
Merge Sort
- Divide an array in half and sort each half

- Merge the two sorted arrays into a single sorted array

- If the computer keeps dividing the array into smaller and smaller arrays,
sorting each half, and merging them back together, it is dramatically faster
than the selection sort.
File MergeSorter.java
File MergeSortTest.java
Merge Sort vs Selection Sort

Merge Sort Timing Vs Selection Sort
Merge sort - rectangles
Selection sort - circles

Analyzing Merge Sort Algorithm
- In an array of size n, count how many times an array element is visited
- Assume n is a power of 2: n = 2m
- calculate the number of visits create the two sub-arrays and then merge
the two sorted arrays
- 3 visits to merge each element or 3n visits
- 2n visits to create the two sub-arrays
- total of 5n visits
- Let T(n) denote the number of visits to sort an array of n
elements then
- T(n) = T(n/2) + T(n/2) + 5n or
- T(n) = 2*T(n/2) + 5n
- The visits for an array of size n/2 is T(n/2)
= 2*T(n/4) + 5n/2
- So T(n) = 2 * 2*T(n/4) +5n + 5n
- The visits for an array of size n/4 is T(n/4)
= 2*T(n/8) + 5n/4
- So T(n) = 2 * 2 * 2*T(n/8) + 5n + 5n
+ 5n
- Repeating the process k times: T(n)
= 2kT(n/2k) +5nk
- since n = 2m, when k = m: T(n)
= 2mT(n/2m) +5nm
- T(n) = nT(1) +5nm
- T(n) = n + 5nlog2(n)
- To establish growth order
- Drop the lower-order term n
- Drop the constant factor 5
- Drop the base of the logarithm since all logarithms are related
by a common factor
- We are left with n log(n)
- Using big-Oh notation: The number of visits is O( nlog(n)
)
Merge Sort Vs Selection Sort
- Selection sort is an O( n2 ) algorithm
- Merge sort is an O( nlog(n) ) algorithm
- The nlog(n) function grows much more slowly than n2
Sorting in a Java Program
- The Arrays class contains
static sort methods
- To sort an array of integers
int[] intArray = . . . ;
Arrays.sort(intArray);
Linear Search
- Also called sequential search
- Examines all values in an array until it finds a match or reaches the end
- Number of visits for a linear search of an array of n elements
- The average search visits n/2 elements
- The maximum visits is n
- A linear search is an O(n) algorithm
File LinearSearcher.java
File LinearSearchTest.java
Binary Search
- Locates a value in a sorted array by determining whether the value occurs
in the first or second half, then repeating the search in one of the halves
- Number of visits to search an sorted array of size n
- We visit one element (the middle element) then search either the left
or right subarray
Thus: T(n) = T(n/2) + 1
- It n is n/2, then T(n/2)
= T(n/4) + 1
- Substituting into the original equation: T(n)
= T(n/4) + 2
- This generalizes to: T(n) = T(n/2k)
+ k
- Assume n is a power of 2, n = 2m
Then: T(n) = T(n/2m)
+ m
- Since m = log2(n), then: T(n)
= 1 + log2(n)
- Binary search is a O( log(n) ) algorithm
File BinarySearcher.java
File BinarySearchTest.java
Searching a Sorted Array in a Java Program
- The Arrays class contains
a static binarySearch method
- The method returns either
- The index of the element if element is found
- Or -k - 1 where k is the position before which the element
should be inserted
- To search an integer array
int[] intArray = { 1, 4, 9 };
int v = 7;
int pos = Arrays.binarySearch(intArray, v);
//returns -3; v should be inserted before position 2
Sorting and Searching Arrays of Objects
- The Arrays class contain
methods for searching or sorting collections of objects that implement the
Comparable interface
- Comparable interface
public interface Comparable
{
int compareTo(Object otherObject);
}
- The call a.compareTo(b)
- Returns a negative number is a should come before b
- Returns 0 if a and b are the same
- Returns a positive number otherwise
CompareTo for BankAccount
public class BankAccount
{
. . .
public int compareTo(Object otherObject)
{
BankAccount other = (BankAccount)otherObject;
if (balance < other.balance) return -1;
if (balance == other.balance return 0;
return 1;
}
. . .
}
CompareTo Method
The method implementation must define a total ordering relationship
- Antisymmetric:
sign( x.compareTo(y) ) = -sign( y.compareTo(x) )
- Reflexive:
x.compareTo(x) = 0
- Transitive:
If x.compareTo(y) <= 0 and y.compareTo(Z) <= 0, then x.compareTo(z) <= 0
Sorting and Searching Arrays of Objects Using Comparator
- The Arrays class contains
a sort method that can sort array of objects that implement the Comparator
interface
- The Comparator interface
public interface Comparator
{
public int compare( Object firstObject, Object secondObject);
}
- If comp is a Comparator object, then the call comp.compare(a,
b)
- Returns a negative number if a should come before b
- Returns 0 if a and b are the same
- Returns a positive number otherwise
Comparator Class for Coins
public class CoinComparator implements Comparator
{
public int compare(Object firstObject, Object secondObject)
{
Coin first = (Coin)firstObject;
Coin second = (Coin)secondObject;
if ( first.getValue() < second.getValue() )
return -1;
if ( first.getValue() == second.getValue() )
return 0;
return 1;
}
}
Sorting an Array of Objects with a Comparator
Coin[] s = . . . ;
Comparator comp = new CoinComparator();
Arrays.sort(coinArray, comp);
Sorting and Searching ArrayLists
- The Collections class contains
static sort and binarySearch
methods that can be used to sort an ArrayList
of objects given a Comparator
object for that class of object
Sorting an ArrayList of Objects with a Comparator
ArrayList Coins = new ArrayList();
//add coins
. . .
Comparator comp = new CoinComparator();
Collections.sort(coins, comp);
File Purse.java
File PurseTest.java