Chapter 14

Sorting and Searching


Chapter Goals

Selection Sort

Sorting an Array of Integers

  1. Find the smallest and swap it with the first element
    5 9 17 11 12

  2. Find the next smallest. It is already in the correct place
    5 9 17 11 12

  3. Find the next smallest and swap it with first element of unsorted portion
    5 9 11 17 12

  4. Repeat
    5 9 11 12 17

  5. When the unsorted portion is of length 1, we are done
    5 9 11 12 17

File SelectionSorter.java

File SelectionSortTester.java

File ArrayUtil.java

Output

65 46 14 52 38 2 96 39 14 33 13 4 24 99 89 77 73 87 36 81
2 4 13 14 14 24 33 36 38 39 46 52 65 73 77 81 87 89 96 99

Self Check

  1. Why do we need the temp variable in the swap method? What would happen if you simply assigned a[i] to a[j] and a[j] to a[i]?
  2. What steps does the selection sort algorithm go through to sort the sequence 6 5 4 3 2 1?

Answers

  1. Dropping the temp variable would not work. Then a[i] and a[j] would end up being the same value.

  2. 1 5
    4
    3
    2 6

    1 2 4
    3
    5
    6

    1 2
    3
    4
    5 6


Profiling the Selection Sort Algorithm

File StopWatch.java

File SelectionSortTimer


Output
 Enter array size: 100000
Elapsed time: 27880 milliseconds

Selection Sort on Various Size Arrays*

n Milliseconds
10,000 772
20,000 3,051
30,000 6,846
40,000 12,188
50,000 19,015
60,000 27,359
* Obtained with a Pentium processor, 1.2 GHz, Java 5.0, Linux

Selection Sort on Various Size Arrays


Selection Sort on Various Size Arrays

Self Check

  1. Approximately how many seconds would it take to sort a data set of 80,000 values?
  2. Look at the graph in Figure 1. What mathematical shape does it resemble?

Answers

  1. Four times as long as 40,000 values, or about 50 seconds.
  2. A parabola.

Analyzing the Performance of the Selection Sort Algorithm

Analyzing the Performance of the Selection Sort Algorithm

Analyzing the Performance of the Selection Sort Algorithm

Self Check

  1. If you increase the size of a data set tenfold, how much longer does it take to sort it with the selection sort algorithm?
  2. How large does n need to be so that n2/2 is bigger than 5n2/2 - 3?

Answers

  1. It takes about 100 times longer.
  2. If n is 4, then n2/2 is 8 and 5n2/2 - 3 is 7.

Insertion Sort

File InsertionSorter.java

Merge Sort

Merge Sort Example

Merge Sort

public void sort()
{
if (a.length <= 1) return;
int[] first = new int[a.length / 2];
int[] second = new int[a.length - first.length];
System.arraycopy(a, 0, first, 0, first.length);
System.arraycopy(a, first.length, second, 0, second.length);
MergeSorter firstSorter = new MergeSorter(first);
MergeSorter secondSorter = new MergeSorter(second);
firstSorter.sort();
secondSorter.sort();
merge(first, second);

}

File MergeSorter.java

File MergeSortTester.java

Output

8 81 48 53 46 70 98 42 27 76 33 24 2 76 62 89 90 5 13 21
2 5 8 13 21 24 27 33 42 46 48 53 62 70 76 76 81 89 90 98

Self Check

  1. Why does only one of the two arraycopy calls at the end of the merge method do any work?
  2. Manually run the merge sort algorithm on the array 8 7 6 5 4 3 2 1.

Answers

  1. When the preceding while loop ends, the loop condition must be false, that is,
    iFirst >= first.length or iSecond >= second.length (De Morgan's Law).
    Then first.length - iFirst <= 0 or iSecond.length - iSecond <= 0.
  2. First sort 8 7 6 5.
    Recursively, first sort 8 7.
    Recursively, first sort 8. It's sorted.
    Sort 7. It's sorted.
    Merge them: 7 8.
    Do the same with 6 5 to get 5 6.
    Merge them to 5 6 7 8.
    Do the same with 4 3 2 1: Sort 4 3 by sorting 4 and 3 and merging them to 3 4.
    Sort 2 1 by sorting 2 and 1 and merging them to 1 2.
    Merge 3 4 and 1 2 to 1 2 3 4.
    Finally, merge 5 6 7 8 and 1 2 3 4 to 1 2 3 4 5 6 7 8.

Analyzing the Merge Sort Algorithm

n Merge Sort (milliseconds) Selection Sort (milliseconds)
10,000 31 772
20,000 47 3,051
30,000 62 6,846
40,000 80 12,188
50,000 97 19,015
60,000 113 27,359

Merge Sort Timing vs. Selection Sort


Analyzing the Merge Sort Algorithm

Analyzing the Merge Sort Algorithm

Analyzing Merge Sort Algorithm

Analyzing Merge Sort Algorithm

Merge Sort Vs Selection Sort

Sorting in a Java Program

Self Check

  1. Given the timing data for the merge sort algorithm in the table at the beginning of this section, how long would it take to sort an array of 100,000 values?
  2. Suppose you have an array double[] values in a Java program. How would you sort it?

Answers

  1. Approximately 100,000 × log(100,000) / 50,000 × log(50,000) = 2 × 5 / 4.7 = 2.13 times the time required for 50,000 values. That's 2.13 × 97 milliseconds or approximately 207 milliseconds.
  2. By calling Arrays.sort(values).

The Quicksort Algorithm

The Quicksort Algorithm

public void sort(int from, int to)
{
if (from >= to) return;
int p = partition(from, to);
sort(from, p);
sort(p + 1, to);
}

The Quicksort Algorithm


The Quicksort Algorithm

private int partition(int from, int to)
{
int pivot = a[from];
int i = from - 1;
int j = to + 1;
while (i < j)
{
i++; while (a[i] < pivot) i++;
j--; while (a[j] > pivot) j--;
if (i < j) swap(i, j);
}
return j;
}

The First Programmer


Searching

File LinearSearcher.java

File LinearSearchTester.java

Output

46 99 45 57 64 95 81 69 11 97 6 85 61 88 29 65 83 88 45 88
Enter number to search for, -1 to quit: 11
Found in position 8

Self Check

  1. Suppose you need to look through 1,000,000 records to find a telephone number. How many records do you expect to search before finding the number?
  2. Why can't you use a "for each" loop for (int element : a) in the search method?

Answers

  1. On average, you'd make 500,000 comparisons.
  2. The search method returns the index at which the match occurs, not the data stored at that location.

Binary Search

Binary Search

File BinarySearcher.java

Binary Search

Binary Search

Searching a Sorted Array in a Program

Self Check

  1. Suppose you need to look through a sorted array with 1,000,000 elements to find a value. Using the binary search algorithm, how many records do you expect to search before finding the value?
  2. Why is it useful that the Arrays.binarySearch method indicates the position where a missing element should be inserted?
  3. Why does Arrays.binarySearch return -k - 1 and not -k to indicate that a value is not present and should be inserted before position k?

Answers

  1. You would search about 20. (The binary log of 1,024 is 10.)
  2. Then you know where to insert it so that the array stays sorted, and you can keep using binary search.
  3. Otherwise, you would not know whether a value is present when the method returns 0.

Sorting Real Data

Sorting Real Data

CompareTo Method

Sorting Real Data

Self Check

  1. Why can't the Arrays.sort method sort an array of Rectangle objects?
  2. What steps would you need to take to sort an array of BankAccount objects by increasing balance?

Answers

  1. The Rectangle class does not implement the Comparable interface.
  2. The BankAccount class needs to implement the Comparable interface. Its compareTo method must compare the bank balances.