Big Java 4

Chapter 14 – Sorting and Searching

Chapter Goals

books sorted by height

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

Selection Sort

santas to be sorted bt height

 

In selection sort, pick the smallest element and swap it with the first one. Pick the smallest element of the remaining ones and swap it with the next one, and so on.

section_1/SelectionSorter.java

Your browser does not support the <object> tag.

section_1/SelectionSortDemo.java

Your browser does not support the <object> tag.

section_1/ArrayUtil.java

Your browser does not support the <object> tag. Typical Program Run:

Self Check 14.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]?

Self Check 14.2

What steps does the selection sort algorithm go through to sort the sequence 6 5 4 3 2 1?

Self Check 14.3

How can you change the selection sort algorithm so that it sorts the elements in descending order (that is, with the largest element at the beginning of the array)?

Self Check 14.4

Suppose we modified the selection sort algorithm to start at the end of the array, working toward the beginning. In each step, the current position is swapped with the minimum. What is the result of this modification?

Profiling the Selection Sort Algorithm

section_2/StopWatch.java

Your browser does not support the <object> tag.

section_2/SelectionSortTimer.java

Your browser does not support the <object> tag. Program Run:

Selection Sort on Various Size Arrays

graph of selection sort time

n Milliseconds
10,000 786
20,000 2,148
30,000 4,796
40,000 9,192
50,000 13,321
60,000 19,299

Doubling the size of the array more than doubles the time needed to sort it .

Self Check 14.5

Approximately how many seconds would it take to sort a data set of 80,000 values?

Self Check 14.6

Look at the graph in Figure 1. What mathematical shape does it resemble?

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 14.7

If you increase the size of a data set tenfold, how much longer does it take to sort it with the selection sort algorithm?

Self Check 14.8

How large does n need to be so that 1/2 n2 is bigger than 5/2 n – 3?

Self Check 14.9

Section 7.3.6 has two algorithms for removing an element from an array of length n. How many array visits does each algorithm require on average?

Self Check 14.10

Describe the number of array visits in Self Check 9 using the big-Oh notation.

Self Check 14.11

What is the big-Oh running time of checking whether an array is already sorted?

Self Check 14.12

Consider this algorithm for sorting an array. Set k to the length of the array. Find the maximum of the first k elements. Remove it, using the second algorithm of Section 7.3.6. Decrement k and place the removed element into the k th position. Stop if k is 1. What is the algorithm’s running time in big-Oh notation?

Common Big-Oh Growth Rates

growth rates

Insertion Sort

Insertion Sort

public class InsertionSorter
{
   /**
      Sorts an array, using insertion sort.
      @param a the array to sort
   */
   public static void sort(int[] a)
   {
      for (int i = 1; i < a.length; i++)
      {
         int next = a[i];
         // Move all larger elements up
         int j = i;
         while (j > 0 && a[j - 1] > next)
         {
            a[j] = a[j - 1];
            j--;
         }
         // Insert the element
         a[j] = next;
      }
   }
}

Insertion Sort

Merge Sort

Merge Sort Example

Merge Sort

public static void sort(int[] a)
{
   if (a.length <= 1) { return; }
   int[] first = new int[a.length / 2];
   int[] second = new int[a.length - first.length];
   // Copy the first half of a into first, the second half into second
   . . .
   sort(first);
   sort(second);
   merge(first, second, a);
}

section_4/MergeSorter.java

Your browser does not support the <object> tag.

section_4/MergeSortDemo.java

Your browser does not support the <object> tag. Typical Program Run:

Self Check 14.13

Why does only one of the two while loops at the end of the merge method do any work?

Self Check 14.14

Manually run the merge sort algorithm on the array 8 7 6 5 4 3 2 1.

Self Check 14.15

The merge sort algorithm processes an array by recursively processing two halves. Describe a similar recursive algorithm for computing the sum of all elements in an array.

Analyzing the Merge Sort Algorithm

Analyzing the Merge Sort Algorithm

Analyzing Merge Sort Algorithm

Analyzing Merge Sort Algorithm

Merge Sort Vs Selection Sort

Merge Sort Timing vs. Selection Sort

merge vs selection sort

n Merge Sort (milliseconds) Selection Sort (milliseconds)
10,000 40 786
20,000 73 2,148
30,000 134 4,796
40,000 170 9,192
50,000 192 13,321
60,000 205 19,299

Self Check 14.16

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?

Self Check 14.17

If you double the size of an array, how much longer will the merge sort algorithm take to sort the new array?

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 static int partition(int[] a, 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) { ArrayUtil.swap(a, i, j); }
    }
    return j;
}

The Quicksort Algorithm - Partitioning

partitioning

extending partition

The Quicksort Algorithm

Searching

section_6_1/LinearSearcher.java

Your browser does not support the <object> tag.

section_6_1/LinearSearchDemo.java

Your browser does not support the <object> tag. Program Run:

Self Check 14.11

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?

Self Check 14.12

Why can't you use a for each loop for (int element : a) in the search method?

Binary Search

Binary Search

section_6_2/BinarySearcher.java

Your browser does not support the <object> tag.

Binary Search

Binary Search

Binary Search

Self Check 14.18

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?

Self Check 14.19

Why can’t you use a “for each” loop for (int element : a) in the search method?

Self Check 14.20

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?

Problem Solving: Estimating the Running Time of an Algorithm - Linear time

Problem Solving: Estimating the Running Time of an Algorithm - Linear time

Problem Solving: Estimating the Running Time of an Algorithm - Quadratic time

Problem Solving: Estimating the Running Time of an Algorithm - Quadratic time

The Triangle Pattern

Problem Solving: Estimating the Running Time of an Algorithm - Logarithmic time

Problem Solving: Estimating the Running Time of an Algorithm - Logarithmic time

Self Check 14.21

What is the “light bulb pattern” of visits in the following algorithm to check whether an array is a palindrome?
for (int i = 0; i < a.length / 2; i++)
{
   if (a[i] != a[a.length - 1 - i]) { return false; }
}
return true;

Self Check 14.22

What is the big-Oh running time of the following algorithm to check whether the first element is duplicated in an array?
for (int i = 1; i < a.length; i++)
{
   if (a[0] == a[i]) { return true; }
}
return false;

Self Check 14.23

What is the big-Oh running time of the following algorithm to check whether an array has a duplicate value?
for (int i = 0; i < a.length; i++)
{
   for (j = i + 1; j < a.length; j++)
   {
      if (a[i] == a[j]) { return true; }
   }
}
return false;

Self Check 14.24

Describe an O(n log(n)) algorithm for checking whether an array has duplicates.

Self Check 14.25

What is the big-Oh running time of the following algorithm to find an element in an n × n array?
for (int i = 0; i < n; i++)
{
   for (j = 0; j < n; j++)
   {
      if (a[i][j] == value) { return true; }
   }
}
return false;

Self Check 14.26

If you apply the algorithm of Section 14.7.4 to an n × n array, what is the big-Oh efficiency of finding the most frequent element in terms of n?

Sorting and Searching in the Java Library - Sorting

Sorting and Searching in the Java Library - Binary Search

Comparing Objects

Comparing Objects

Self Check 14.27

Why can't the Arrays.sort method sort an array of Rectangle objects?

Self Check 14.28

What steps would you need to take to sort an array of BankAccount objects by increasing balance?

Self Check 14.29

Why is it useful that the Arrays.binarySearch method indicates the position where a missing element should be inserted?

Self Check 14.30

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?