2005 pearson education, inc. all rights reserved. 1 16 searching and sorting
TRANSCRIPT
2
2005 Pearson Education Inc. All rights reserved.
With sobs and tears he sorted outThose of the largest size ...
— Lewis Carroll
Attempt the end, and never stand to doubt;Nothing’s so hard, but search will find it out.
— Robert Herrick
‘Tis in my memory lock’d,And you yourself shall keep the key of it.
— William Shakespeare
It is an immutable law in business that words are words, explanations are explanations, promises are promises — but only performance is reality.
— Harold S. Green
3
2005 Pearson Education, Inc. All rights reserved.
OBJECTIVESIn this chapter you will learn: To search for a given value in an array using
linear search and binary search. To sort arrays using the iterative selection and
insertion sort algorithms. To sort arrays using the recursive merge sort
algorithm. To determine the efficiency of searching and
sorting algorithms.
4
2005 Pearson Education, Inc. All rights reserved.
16.1 Introduction
16.2 Searching Algorithms
16.2.1 Linear Search
16.2.2 Binary Search
16.3 Sorting Algorithms
16.3.1 Selection Sort
16.3.2 Insertion Sort
16.3.3 Merge Sort
16.4 Invariants
16.5 Wrap-up
5
2005 Pearson Education, Inc. All rights reserved.
16.1 Introduction
• Searching– Determining whether a search key is present in data
• Sorting– Places data in order based on one or more sort keys
6
2005 Pearson Education, Inc. All rights reserved.
Fig. 16.1 | Searching and sorting algorithms in this text.
Chapter Algorithm Location
Searching Algorithms:
16 Linear Search Section 16.2.1
Binary Search Section 16.2.2 Recursive Linear Search Exercise 16.8 Recursive Binary Search Exercise 16.9
17 Linear search of a List Exercise 17.21
Binary tree search Exercise 17.23
19 binarySearch method of class Collections Fig. 19.14
Sorting Algorithms:
16 Selection Sort Section 16.3.1 Insertion Sort Section 16.3.2
Recursive Merge Sort Section 16.3.3 Bubble Sort Exercises 16.3 and 16.4
Bucket Sort Exercise 16.7
Recursive Quicksort Exercise 16.10 17 Binary tree sort Section 17.9
19 sort method of class Collections Fig. 19.8–Fig. 19.11
SortedSet collection Fig. 19.19
7
2005 Pearson Education, Inc. All rights reserved.
16.2 Searching Algorithms
• Examples of searching– Looking up a phone number
– Accessing a Web site
– Checking a word in the dictionary
8
2005 Pearson Education, Inc. All rights reserved.
16.2.1 Linear Search
• Linear search– Searches each element sequentially
– If search key is not present• Tests each element
• When algorithm reaches end of array, informs user search key is not present
– If search key is present• Test each element until it finds a match
9
2005 Pearson Education, Inc. All rights reserved.
Outline
LinearArray.java
(1 of 2)
1 // Fig 16.2: LinearArray.java
2 // Class that contains an array of random integers and a method
3 // that will search that array sequentially
4 import java.util.Random;
5
6 public class LinearArray
7 {
8 private int[] data; // array of values
9 private static Random generator = new Random();
10
11 // create array of given size and fill with random numbers
12 public LinearArray( int size )
13 {
14 data = new int[ size ]; // create space for array
15
16 // fill array with random ints in range 10-99
17 for ( int i = 0; i < size; i++ )
18 data[ i ] = 10 + generator.nextInt( 90 );
19 } // end LinearArray constructor 20
10
2005 Pearson Education, Inc. All rights reserved.
Outline
LinearArray.java
(2 of 2)
21 // perform a linear search on the data
22 public int linearSearch( int searchKey )
23 {
24 // loop through array sequentially
25 for ( int index = 0; index < data.length; index++ )
26 if ( data[ index ] == searchKey )
27 return index; // return index of integer
28
29 return -1; // integer was not found
30 } // end method linearSearch
31
32 // method to output values in array
33 public String toString()
34 {
35 StringBuffer temporary = new StringBuffer();
36
37 // iterate through array
38 for ( int element : data )
39 temporary.append( element + " " );
40
41 temporary.append( "\n" ); // add endline character
42 return temporary.toString();
43 } // end method toString
44 } // end class LinearArray
Iterate through array
Test each element sequentially
Return index containing search key
11
2005 Pearson Education, Inc. All rights reserved.
Outline
LinearSearchTest.java
(1 of 2)
1 // Fig 16.3: LinearSearchTest.java
2 // Sequentially search an array for an item.
3 import java.util.Scanner;
4
5 public class LinearSearchTest
6 {
7 public static void main( String args[] )
8 {
9 // create Scanner object to input data
10 Scanner input = new Scanner( System.in );
11
12 int searchInt; // search key
13 int position; // location of search key in array
14
15 // create array and output it
16 LinearArray searchArray = new LinearArray( 10 );
17 System.out.println( searchArray ); // print array
18
19 // get input from user
20 System.out.print(
21 "Please enter an integer value (-1 to quit): " );
22 searchInt = input.nextInt(); // read first int from user
23
24 // repeatedly input an integer; -1 terminates the program
25 while ( searchInt != -1 )
26 {
27 // perform linear search
28 position = searchArray.linearSearch( searchInt ); 29
12
2005 Pearson Education, Inc. All rights reserved.
Outline
LinearSearchTest.java
(2 of 2)
30 if ( position == -1 ) // integer was not found
31 System.out.println( "The integer " + searchInt +
32 " was not found.\n" );
33 else // integer was found
34 System.out.println( "The integer " + searchInt +
35 " was found in position " + position + ".\n" );
36
37 // get input from user
38 System.out.print(
39 "Please enter an integer value (-1 to quit): " );
40 searchInt = input.nextInt(); // read next int from user
41 } // end while
42 } // end main
43 } // end class LinearSearchTest
16 35 68 10 48 36 81 60 84 21 Please enter an integer value (-1 to quit): 48 The integer 48 was found in position 4. Please enter an integer value (-1 to quit): 60 The integer 60 was found in position 7. Please enter an integer value (-1 to quit): 33 The integer 33 was not found. Please enter an integer value (-1 to quit): -1
13
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Linear Search
• Big O Notation– Indicates the worst-case run time for an algorithm
– In other words, how hard an algorithm has to work to solve a problem
– Constant run time• O(1)
• Does not grow as the size of the array increases
– Linear run time• O(n)
• Grows proportional to the size of the array
14
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Linear Search
• Big O Notation– Constant run time
• O(1)
• Does not grow as the size of the array increases
– Linear run time• O(n)
• Grows proportional to the size of the array
– Quadratic run time• O(n2)
• Grows proportional to the square of the size of the array
15
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Linear Search
• Linear search algorithm– O(n)
– Worst case: algorithm checks every element before being able to determine if the search key is not present
– Grows proportional to the size of the array
16
2005 Pearson Education, Inc. All rights reserved.
Performance Tip 16.1
Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realize maximum performance.
17
2005 Pearson Education, Inc. All rights reserved.
16.2.2 Binary Search
• Binary search– More efficient than linear search
– Requires elements to be sorted
– Tests the middle element in an array• If it is the search key, algorithm returns
• Otherwise, if the search key is smaller, eliminates larger half of array
• If the search key is larger, eliminates smaller half of array
– Each iteration eliminates half of the remaining elements
18
2005 Pearson Education, Inc. All rights reserved.
Outline
BinaryArray.java
(1 of 3)
1 // Fig 16.4: BinaryArray.java
2 // Class that contains an array of random integers and a method
3 // that uses binary search to find an integer.
4 import java.util.Random;
5 import java.util.Arrays;
6
7 public class BinaryArray
8 {
9 private int[] data; // array of values
10 private static Random generator = new Random();
11
12 // create array of given size and fill with random integers
13 public BinaryArray( int size )
14 {
15 data = new int[ size ]; // create space for array
16
17 // fill array with random ints in range 10-99
18 for ( int i = 0; i < size; i++ )
19 data[ i ] = 10 + generator.nextInt( 90 );
20
21 Arrays.sort( data );
22 } // end BinaryArray constructor 23
19
2005 Pearson Education, Inc. All rights reserved.
Outline
BinaryArray.java
(2 of 3)
24 // perform a binary search on the data
25 public int binarySearch( int searchElement )
26 {
27 int low = 0; // low end of the search area
28 int high = data.length - 1; // high end of the search area
29 int middle = ( low + high + 1 ) / 2; // middle element
30 int location = -1; // return value; -1 if not found
31
32 do // loop to search for element
33 {
34 // print remaining elements of array
35 System.out.print( remainingElements( low, high ) );
36
37 // output spaces for alignment
38 for ( int i = 0; i < middle; i++ )
39 System.out.print( " " );
40 System.out.println( " * " ); // indicate current middle
41
42 // if the element is found at the middle
43 if ( searchElement == data[ middle ] )
44 location = middle; // location is the current middle
45
46 // middle element is too high
47 else if ( searchElement < data[ middle ] )
48 high = middle - 1; // eliminate the higher half
49 else // middle element is too low
50 low = middle + 1; // eliminate the lower half 51
Store high, low and middle of remaining array to search
Loop until key is found or no elements left to search
If search element is the middle element
Return middle element
If search element is less than middle element
Eliminate higher half
Else, eliminate lower half
20
2005 Pearson Education, Inc. All rights reserved.
Outline
BinaryArray.java
(3 of 3)
52 middle = ( low + high + 1 ) / 2; // recalculate the middle
53 } while ( ( low <= high ) && ( location == -1 ) );
54
55 return location; // return location of search key
56 } // end method binarySearch
57
58 // method to output certain values in array
59 public String remainingElements( int low, int high )
60 {
61 StringBuffer temporary = new StringBuffer();
62
63 // output spaces for alignment
64 for ( int i = 0; i < low; i++ )
65 temporary.append( " " );
66
67 // output elements left in array
68 for ( int i = low; i <= high; i++ )
69 temporary.append( data[ i ] + " " );
70
71 temporary.append( "\n" );
72 return temporary.toString();
73 } // end method remainingElements
74
75 // method to output values in array
76 public String toString()
77 {
78 return remainingElements( 0, data.length - 1 );
79 } // end method toString
80 } // end class BinaryArray
Update middle of array
Return location of element
21
2005 Pearson Education, Inc. All rights reserved.
Outline
BinarySearchTest.java
(1 of 3)
1 // Fig 16.5: BinarySearchTest.java
2 // Use binary search to locate an item in an array.
3 import java.util.Scanner;
4
5 public class BinarySearchTest
6 {
7 public static void main( String args[] )
8 {
9 // create Scanner object to input data
10 Scanner input = new Scanner( System.in );
11
12 int searchInt; // search key
13 int position; // location of search key in array
14
15 // create array and output it
16 BinaryArray searchArray = new BinaryArray( 15 );
17 System.out.println( searchArray );
18
22
2005 Pearson Education, Inc. All rights reserved.
Outline
BinarySearchTest.java
(2 of 3)
19 // get input from user
20 System.out.print(
21 "Please enter an integer value (-1 to quit): " );
22 searchInt = input.nextInt(); // read an int from user
23 System.out.println();
24
25 // repeatedly input an integer; -1 terminates the program
26 while ( searchInt != -1 )
27 {
28 // use binary search to try to find integer
29 position = searchArray.binarySearch( searchInt );
30
31 // return value of -1 indicates integer was not found
32 if ( position == -1 )
33 System.out.println( "The integer " + searchInt +
34 " was not found.\n" );
35 else
36 System.out.println( "The integer " + searchInt +
37 " was found in position " + position + ".\n" );
38
39 // get input from user
40 System.out.print(
41 "Please enter an integer value (-1 to quit): " );
42 searchInt = input.nextInt(); // read an int from user
43 System.out.println();
44 } // end while
45 } // end main
46 } // end class BinarySearchTest
23
2005 Pearson Education, Inc. All rights reserved.
Outline
BinarySearchTest.java
(3 of 3)
13 23 24 34 35 36 38 42 47 51 68 74 75 85 97 Please enter an integer value (-1 to quit): 23 13 23 24 34 35 36 38 42 47 51 68 74 75 85 97 * 13 23 24 34 35 36 38 * 13 23 24 * The integer 23 was found in position 1. Please enter an integer value (-1 to quit): 75 13 23 24 34 35 36 38 42 47 51 68 74 75 85 97 * 47 51 68 74 75 85 97 * 75 85 97 * 75 * The integer 75 was found in position 12. Please enter an integer value (-1 to quit): 52 13 23 24 34 35 36 38 42 47 51 68 74 75 85 97 * 47 51 68 74 75 85 97 * 47 51 68 * 68 * The integer 52 was not found. Please enter an integer value (-1 to quit): -1
24
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Binary Search
• Binary search– Each comparison halves the size of the remaining array
– Results in O(log n)
– Called logarithmic run time
25
2005 Pearson Education, Inc. All rights reserved.
16.3 Sorting Algorithms
• Sorting data– Placing data into some particular order
• A bank sorts checks by account number
• Telephone companies sort accounts by name
– End result is always the same – a sorted array
– Choice of algorithm affects how you achieve the result and, most importantly, how fast you achieve the result
26
2005 Pearson Education, Inc. All rights reserved.
16.3.1 Selection Sort
• Selection sort– Simple, but inefficient sorting algorithm
– First iteration selects smallest element in array and swaps it with the first element
– Each iteration selects the smallest remaining unsorted element and swaps it with the next element at the front of the array
– After i iterations, the smallest i elements will be sorted in the first i elements of the array
27
2005 Pearson Education, Inc. All rights reserved.
Outline
SelectionSort.java
(1 of 3)
1 // Fig 16.6: SelectionSort.java
2 // Class that creates an array filled with random integers.
3 // Provides a method to sort the array with selection sort.
4 import java.util.Random;
5
6 public class SelectionSort
7 {
8 private int[] data; // array of values
9 private static Random generator = new Random();
10
11 // create array of given size and fill with random integers
12 public SelectionSort( int size )
13 {
14 data = new int[ size ]; // create space for array
15
16 // fill array with random ints in range 10-99
17 for ( int i = 0; i < size; i++ )
18 data[ i ] = 10 + generator.nextInt( 90 );
19 } // end SelectionSort constructor
20
21 // sort array using selection sort
22 public void sort()
23 {
24 int smallest; // index of smallest element
25
26 // loop over data.length - 1 elements
27 for ( int i = 0; i < data.length - 1; i++ )
28 {
29 smallest = i; // first index of remaining array 30
Variable to store index of smallest element
Loop length – 1 times
28
2005 Pearson Education, Inc. All rights reserved.
Outline
SelectionSort.java
(2 of 3)
31 // loop to find index of smallest element
32 for ( int index = i + 1; index < data.length; index++ )
33 if ( data[ index ] < data[ smallest ] )
34 smallest = index;
35
36 swap( i, smallest ); // swap smallest element into position
37 printPass( i + 1, smallest ); // output pass of algorithm
38 } // end outer for
39 } // end method sort
40
41 // helper method to swap values in two elements
42 public void swap( int first, int second )
43 {
44 int temporary = data[ first ]; // store first in temporary
45 data[ first ] = data[ second ]; // replace first with second
46 data[ second ] = temporary; // put temporary in second
47 } // end method swap
48
49 // print a pass of the algorithm
50 public void printPass( int pass, int index )
51 {
52 System.out.print( String.format( "after pass %2d: ", pass ) );
53
54 // output elements till selected item
55 for ( int i = 0; i < index; i++ )
56 System.out.print( data[ i ] + " " );
57
58 System.out.print( data[ index ] + "* " ); // indicate swap 59
Loop over remaining elements
Locate smallest remaining element
Swap smallest element with first unsorted element
29
2005 Pearson Education, Inc. All rights reserved.
Outline
SelectionSort.java
(3 of 3)
60 // finish outputting array
61 for ( int i = index + 1; i < data.length; i++ )
62 System.out.print( data[ i ] + " " );
63
64 System.out.print( "\n " ); // for alignment
65
66 // indicate amount of array that is sorted
67 for ( int j = 0; j < pass; j++ )
68 System.out.print( "-- " );
69 System.out.println( "\n" ); // add endline
70 } // end method indicateSelection
71
72 // method to output values in array
73 public String toString()
74 {
75 StringBuffer temporary = new StringBuffer();
76
77 // iterate through array
78 for ( int element : data )
79 temporary.append( element + " " );
80
81 temporary.append( "\n" ); // add endline character
82 return temporary.toString();
83 } // end method toString
84 } // end class SelectionSort
30
2005 Pearson Education, Inc. All rights reserved.
Outline
SelectionSortTest.java
(1 of 2)
1 // Fig 16.7: SelectionSortTest.java
2 // Test the selection sort class.
3
4 public class SelectionSortTest
5 {
6 public static void main( String[] args )
7 {
8 // create object to perform selection sort
9 SelectionSort sortArray = new SelectionSort( 10 );
10
11 System.out.println( "Unsorted array:" );
12 System.out.println( sortArray ); // print unsorted array
13
14 sortArray.sort(); // sort array
15
16 System.out.println( "Sorted array:" );
17 System.out.println( sortArray ); // print sorted array
18 } // end main
19 } // end class SelectionSortTest
31
2005 Pearson Education, Inc. All rights reserved.
Outline
SelectionSortTest.java
(2 of 2)
Unsorted array: 61 87 80 58 40 50 20 13 71 45 after pass 1: 13 87 80 58 40 50 20 61* 71 45 -- after pass 2: 13 20 80 58 40 50 87* 61 71 45 -- -- after pass 3: 13 20 40 58 80* 50 87 61 71 45 -- -- -- after pass 4: 13 20 40 45 80 50 87 61 71 58* -- -- -- -- after pass 5: 13 20 40 45 50 80* 87 61 71 58 -- -- -- -- -- after pass 6: 13 20 40 45 50 58 87 61 71 80* -- -- -- -- -- -- after pass 7: 13 20 40 45 50 58 61 87* 71 80 -- -- -- -- -- -- -- after pass 8: 13 20 40 45 50 58 61 71 87* 80 -- -- -- -- -- -- -- -- after pass 9: 13 20 40 45 50 58 61 71 80 87* -- -- -- -- -- -- -- -- -- Sorted array: 13 20 40 45 50 58 61 71 80 87
32
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Selection Sort
• Selection sort– Outer for loop iterates over n – 1 elements
– Inner for loop iterates over remaining elements in the array
– Results in O(n2)
33
2005 Pearson Education, Inc. All rights reserved.
16.3.2 Insertion Sort
• Insertion sort– Another simple, but inefficient sorting algorithm
– First pass takes the second element and inserts it into the correct order with the first element
– Each iteration takes the next element in the array and inserts it into the sorted elements at the beginning of the array
– After i iterations, the first i elements of the array are in sorted order
34
2005 Pearson Education, Inc. All rights reserved.
Outline
InsertionSort.java
(1 of 4)
1 // Fig 16.8: InsertionSort.java
2 // Class that creates an array filled with random integers.
3 // Provides a method to sort the array with insertion sort.
4 import java.util.Random;
5
6 public class InsertionSort
7 {
8 private int[] data; // array of values
9 private static Random generator = new Random();
10
11 // create array of given size and fill with random integers
12 public InsertionSort( int size )
13 {
14 data = new int[ size ]; // create space for array
15
16 // fill array with random ints in range 10-99
17 for ( int i = 0; i < size; i++ )
18 data[ i ] = 10 + generator.nextInt( 90 );
19 } // end InsertionSort constructor
20
35
2005 Pearson Education, Inc. All rights reserved.
Outline
InsertionSort.java
(2 of 4)
21 // sort array using insertion sort
22 public void sort()
23 {
24 int insert; // temporary variable to hold element to insert
25
26 // loop over data.length - 1 elements
27 for ( int next = 1; next < data.length; next++ )
28 {
29 // store value in current element
30 insert = data[ next ];
31
32 // initialize location to place element
33 int moveItem = next;
34
35 // search for place to put current element
36 while ( moveItem > 0 && data[ moveItem - 1 ] > insert )
37 {
38 // shift element right one slot
39 data[ moveItem ] = data[ moveItem - 1 ];
40 moveItem--;
41 } // end while
42
43 data[ moveItem ] = insert; // place inserted element
44 printPass( next, moveItem ); // output pass of algorithm
45 } // end for
46 } // end method sort
47
Declare variable to store element to be inserted
Iterate over length – 1 elements
Store value to insert
Search for location to place inserted element
Move one element to the right
Decrement location to insert element
Insert element into sorted place
36
2005 Pearson Education, Inc. All rights reserved.
Outline
InsertionSort.java
(3 of 4)
48 // print a pass of the algorithm
49 public void printPass( int pass, int index )
50 {
51 System.out.print( String.format( "after pass %2d: ", pass ) );
52
53 // output elements till swapped item
54 for ( int i = 0; i < index; i++ )
55 System.out.print( data[ i ] + " " );
56
57 System.out.print( data[ index ] + "* " ); // indicate swap
58
59 // finish outputting array
60 for ( int i = index + 1; i < data.length; i++ )
61 System.out.print( data[ i ] + " " );
62
63 System.out.print( "\n " ); // for alignment
64
65 // indicate amount of array that is sorted
66 for( int i = 0; i <= pass; i++ )
67 System.out.print( "-- " );
68 System.out.println( "\n" ); // add endline
69 } // end method printPass
70
37
2005 Pearson Education, Inc. All rights reserved.
Outline
InsertionSort.java
(4 of 4)
71 // method to output values in array
72 public String toString()
73 {
74 StringBuffer temporary = new StringBuffer();
75
76 // iterate through array
77 for ( int element : data )
78 temporary.append( element + " " );
79
80 temporary.append( "\n" ); // add endline character
81 return temporary.toString();
82 } // end method toString
83 } // end class InsertionSort
38
2005 Pearson Education, Inc. All rights reserved.
Outline
InsertionSortTest.java
(1 of 2)
1 // Fig 16.9: InsertionSortTest.java
2 // Test the insertion sort class.
3
4 public class InsertionSortTest
5 {
6 public static void main( String[] args )
7 {
8 // create object to perform selection sort
9 InsertionSort sortArray = new InsertionSort( 10 );
10
11 System.out.println( "Unsorted array:" );
12 System.out.println( sortArray ); // print unsorted array
13
14 sortArray.sort(); // sort array
15
16 System.out.println( "Sorted array:" );
17 System.out.println( sortArray ); // print sorted array
18 } // end main
19 } // end class InsertionSortTest
39
2005 Pearson Education, Inc. All rights reserved.
Outline
InsertionSortTest.java
(2 of 2)
Unsorted array: 40 17 45 82 62 32 30 44 93 10 after pass 1: 17* 40 45 82 62 32 30 44 93 10 -- -- after pass 2: 17 40 45* 82 62 32 30 44 93 10 -- -- -- after pass 3: 17 40 45 82* 62 32 30 44 93 10 -- -- -- -- after pass 4: 17 40 45 62* 82 32 30 44 93 10 -- -- -- -- -- after pass 5: 17 32* 40 45 62 82 30 44 93 10 -- -- -- -- -- -- after pass 6: 17 30* 32 40 45 62 82 44 93 10 -- -- -- -- -- -- -- after pass 7: 17 30 32 40 44* 45 62 82 93 10 -- -- -- -- -- -- -- -- after pass 8: 17 30 32 40 44 45 62 82 93* 10 -- -- -- -- -- -- -- -- -- after pass 9: 10* 17 30 32 40 44 45 62 82 93 -- -- -- -- -- -- -- -- -- -- Sorted array: 10 17 30 32 40 44 45 62 82 93
40
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Insertion Sort
• Insertion sort– Outer for loop iterates over n – 1 elements
– Inner while loop iterates over preceding elements of array
– Results in O(n2)
41
2005 Pearson Education, Inc. All rights reserved.
16.3.3 Merge Sort
• Merge sort– More efficient sorting algorithm, but also more complex
– Splits array into two approximately equal sized subarrays, sorts each subarray, then merges the subarrays
– The following implementation is recursive• Base case is a one-element array which cannot be unsorted
• Recursion step splits an array into two pieces, sorts each piece, then merges the sorted pieces
42
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSort.java
(1 of 5)
1 // Figure 16.10: MergeSort.java
2 // Class that creates an array filled with random integers.
3 // Provides a method to sort the array with merge sort.
4 import java.util.Random;
5
6 public class MergeSort
7 {
8 private int[] data; // array of values
9 private static Random generator = new Random();
10
11 // create array of given size and fill with random integers
12 public MergeSort( int size )
13 {
14 data = new int[ size ]; // create space for array
15
16 // fill array with random ints in range 10-99
17 for ( int i = 0; i < size; i++ )
18 data[ i ] = 10 + generator.nextInt( 90 );
19 } // end MergeSort constructor
20
21 // calls recursive split method to begin merge sorting
22 public void sort()
23 {
24 sortArray( 0, data.length - 1 ); // split entire array
25 } // end method sort 26
Call recursive helper method
43
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSort.java
(2 of 5)
27 // splits array, sorts subarrays and merges subarrays into sorted array
28 private void sortArray( int low, int high )
29 {
30 // test base case; size of array equals 1
31 if ( ( high - low ) >= 1 ) // if not base case
32 {
33 int middle1 = ( low + high ) / 2; // calculate middle of array
34 int middle2 = middle1 + 1; // calculate next element over
35
36 // output split step
37 System.out.println( "split: " + subarray( low, high ) );
38 System.out.println( " " + subarray( low, middle1 );
39 System.out.println( " " + subarray( middle2, high ) );
40 System.out.println();
41
42 // split array in half; sort each half (recursive calls)
43 sortArray( low, middle1 ); // first half of array
44 sortArray( middle2, high ); // second half of array
45
46 // merge two sorted arrays after split calls return
47 merge ( low, middle1, middle2, high );
48 } // end if
49 } // end method split 50
Test for base case
Compute middle of array
Compute element one spot to right of middle
Recursively sort first half of array
Recursively sort second half of array
Merge the two sorted subarrays
44
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSort.java
(3 of 5)
51 // merge two sorted subarrays into one sorted subarray
52 private void merge( int left, int middle1, int middle2, int right )
53 {
54 int leftIndex = left; // index into left subarray
55 int rightIndex = middle2; // index into right subarray
56 int combinedIndex = left; // index into temporary working array
57 int[] combined = new int[ data.length ]; // working array
58
59 // output two subarrays before merging
60 System.out.println( "merge: " + subarray( left, middle1 ) );
61 System.out.println( " " + subarray( middle2, right ) );
62
63 // merge arrays until reaching end of either
64 while ( leftIndex <= middle1 && rightIndex <= right )
65 {
66 // place smaller of two current elements into result
67 // and move to next space in arrays
68 if ( data[ leftIndex ] <= data[ rightIndex ] )
69 combined[ combinedIndex++ ] = data[ leftIndex++ ];
70 else
71 combined[ combinedIndex++ ] = data[ rightIndex++ ];
72 } // end while 73
Index of element in left array
Index of element in right array
Index to place element in combined array
Array to hold sorted elements
Loop until reach end of either array
Determine smaller of two elements
Place smaller element in combined array
45
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSort.java
(4 of 5)
74 // if left array is empty
75 if ( leftIndex == middle2 )
76 // copy in rest of right array
77 while ( rightIndex <= right )
78 combined[ combinedIndex++ ] = data[ rightIndex++ ];
79 else // right array is empty
80 // copy in rest of left array
81 while ( leftIndex <= middle1 )
82 combined[ combinedIndex++ ] = data[ leftIndex++ ];
83
84 // copy values back into original array
85 for ( int i = left; i <= right; i++ )
86 data[ i ] = combined[ i ];
87
88 // output merged array
89 System.out.println( " " + subarray( left, right ) );
90 System.out.println();
91 } // end method merge 92
If left array is empty
Fill with elements of right array
If right array is empty
Fill with elements of left array
Copy values back to original array
46
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSort.java
(5 of 5)
93 // method to output certain values in array
94 public String subarray( int low, int high )
95 {
96 StringBuffer temporary = new StringBuffer();
97
98 // output spaces for alignment
99 for ( int i = 0; i < low; i++ )
100 temporary.append( " " );
101
102 // output elements left in array
103 for ( int i = low; i <= high; i++ )
104 temporary.append( " " + data[ i ] );
105
106 return temporary.toString();
107 } // end method subarray
108
109 // method to output values in array
110 public String toString()
111 {
112 return subarray( 0, data.length - 1 );
113 } // end method toString
114 } // end class MergeSort
47
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSortTest.java
(1 of 4)
1 // Figure 16.11: MergeSortTest.java
2 // Test the merge sort class.
3
4 public class MergeSortTest
5 {
6 public static void main( String[] args )
7 {
8 // create object to perform merge sort
9 MergeSort sortArray = new MergeSort( 10 );
10
11 // print unsorted array
12 System.out.println( "Unsorted:" + sortArray + "\n" );
13
14 sortArray.sort(); // sort array
15
16 // print sorted array
17 System.out.println( "Sorted: " + sortArray );
18 } // end main
19 } // end class MergeSortTest
48
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSortTest.java
(2 of 4)
Unsorted: 75 56 85 90 49 26 12 48 40 47 split: 75 56 85 90 49 26 12 48 40 47 75 56 85 90 49 26 12 48 40 47 split: 75 56 85 90 49 75 56 85 90 49 split: 75 56 85 75 56 85 split: 75 56 75 56
49
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSortTest.java
(3 of 4)
merge: 75 56 56 75 merge: 56 75 85 56 75 85 split: 90 49 90 49 merge: 90 49 49 90 merge: 56 75 85 49 90 49 56 75 85 90 split: 26 12 48 40 47 26 12 48 40 47 split: 26 12 48 26 12 48 split: 26 12 26 12
50
2005 Pearson Education, Inc. All rights reserved.
Outline
MergeSortTest.java
(3 of 4)
merge: 26 12 12 26 merge: 12 26 48 12 26 48 split: 40 47 40 47 merge: 40 47 40 47 merge: 12 26 48 40 47 12 26 40 47 48 merge: 49 56 75 85 90 12 26 40 47 48 49 56 75 85 90 Sorted: 12 26 40 47 48 49 56 75 85 90
51
2005 Pearson Education, Inc. All rights reserved.
Efficiency of Merge Sort
• Merge sort– Far more efficient that selection sort or insertion sort
– Last merge requires n – 1 comparisons to merge entire array
– Each lower level has twice as many calls to method merge, with each call operating on an array half the size which results in O(n) total comparisons
– There will be O(log n) levels
– Results in O(n log n)
52
2005 Pearson Education, Inc. All rights reserved.
Fig. 16.12 | Searching and sorting algorithms with Big O values.
Algorithm Location Big O
Searching Algorithms:
Linear Search Section 16.2.1 O(n) Binary Search Section 16.2.2 O(log n) Recursive Linear Search Exercise 16.8 O(n) Recursive Binary Search Exercise 16.9 O(log n)
Sorting Algorithms:
Selection Sort Section 16.3.1 O(n2) Insertion Sort Section 16.3.2 O(n2) Merge Sort Section 16.3.3 O(n log n) Bubble Sort Exercises 16.3 and 16.4 O(n2)
53
2005 Pearson Education, Inc. All rights reserved.
Fig. 16.13 | Number of comparisons for common Big O notations.
n = O(log n) O(n) O(n log n) O(n2)
1 0 1 0 1
2 1 2 2 4
3 1 3 3 9
4 1 4 4 16
5 1 5 5 25
10 1 10 10 100
100 2 100 200 10000
1,000 3 1000 3000 106
1,000,000 6 1000000 6000000 1012
1,000,000,000 9 1000000000 9000000000 1018
54
2005 Pearson Education, Inc. All rights reserved.
16.4 Invariants
• Invariant– Is an assertion that is true before and after a portion of
your code executes
– Most common type is a loop invariant
55
2005 Pearson Education, Inc. All rights reserved.
16.4 Invariants
• Loop invariant remains true:– Before the execution of the loop
– After every iteration of the loop body
– When the loop terminates
56
2005 Pearson Education, Inc. All rights reserved.
16.4 Invariants
• Four steps to developing a loop from a loop invariant
– Set initial values for any loop control variables
– Determine the condition that causes the loop to terminate
– Modify the control variable so the loop progresses toward termination
– Check that the invariant remains true at the end of each iteration