Classic Data Structures, and Algorithm Complexity Issues:

25 downloads 838 Views 472KB Size Report
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.
1

16 Classic Data Structures, and Algorithm Complexity Issues: Searching and Sorting  2005 Pearson Education, Inc. All rights reserved.

2

16.1

Introduction

16.2

Searching Algorithms

16.3

16.2.1

Linear Search

16.2.2

Binary Search

Sorting Algorithms 16.3.1

Selection Sort

16.3.2

Insertion Sort

16.3.3

Merge Sort

16.4

Invariants

16.5

Wrap-up

 2005 Pearson Education, Inc. All rights reserved.

3

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

 2005 Pearson Education, Inc. All rights reserved.

4

Chapter

Algorithm

Location

Searching Algorithms: 16

17 19

Linear Search Binary Search Recursive Linear Search Recursive Binary Search Linear search of a List Binary tree search binarySearch method of class Collections

Section 16.2.1 Section 16.2.2 Exercise 16.8 Exercise 16.9 Exercise 17.21 Exercise 17.23 Fig. 19.14

Sorting Algorithms: 16

17 19

Selection Sort Insertion Sort Recursive Merge Sort Bubble Sort Bucket Sort Recursive Quicksort Binary tree sort

Section 16.3.1 Section 16.3.2 Section 16.3.3 Exercises 16.3 and 16.4 Exercise 16.7 Exercise 16.10 Section 17.9

sort method of class Collections Fig. 19.8–Fig. 19.11 SortedSet collection Fig. 19.19

Fig. 16.1 | Searching and sorting algorithms in this text.

 2005 Pearson Education, Inc. All rights reserved.

5

16.2 Searching Algorithms • Examples of searching – Looking up a phone number – Accessing a Web site – Checking a word in the dictionary

 2005 Pearson Education, Inc. All rights reserved.

6

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

 2005 Pearson Education, Inc. All rights reserved.

1

// Fig 16.2: LinearArray.java

2 3 4 5 6

// Class that contains an array of random integers and a method // that will search that array sequentially import java.util.Random;

Outline

public class LinearArray

LinearArray.java

7 { 8 9 10 11 12

private int[] data; // array of values private static Random generator = new Random();

7

(1 of 2)

// create array of given size and fill with random numbers public LinearArray( int size )

13 14 15 16 17

{

18 19 20

data[ i ] = 10 + generator.nextInt( 90 ); } // end LinearArray constructor

data = new int[ size ]; // create space for array // fill array with random ints in range 10-99 for ( int i = 0; i < size; i++ )

 2005 Pearson Education, Inc. All rights reserved.

21 22 23 24 25 26 27 28 29

// perform a linear search on the data public int linearSearch( int searchKey ) { // loop through array sequentially for ( int index = 0; index < data.length; index++ ) if ( data[ index ] == searchKey ) return index; // return index of integer

} // end method linearSearch

33 34 35

public String toString() { StringBuffer temporary = new StringBuffer();

39 40 41

Iterate through array

LinearArray.java

Test each element sequentially

(2 of 2)

return -1; // integer was not found

30 31 32

36 37 38

Outline

8

Return index containing search key

// method to output values in array

// iterate through array for ( int element : data ) temporary.append( element + " " ); temporary.append( "\n" ); // add endline character

42 return temporary.toString(); 43 } // end method toString 44 } // end class LinearArray

 2005 Pearson Education, Inc. All rights reserved.

1

// Fig 16.3: LinearSearchTest.java

2

// Sequentially search an array for an item.

3 4

import java.util.Scanner;

5

public class LinearSearchTest

6

{

7 8

public static void main( String args[] ) {

9

// create Scanner object to input data

10 11

Scanner input = new Scanner( System.in );

12

int searchInt; // search key

13 14 15

int position; // location of search key in array

16

LinearArray searchArray = new LinearArray( 10 );

17

System.out.println( searchArray ); // print array

18 19

// get input from user

20

System.out.print(

21 22

"Please enter an integer value (-1 to quit): " ); searchInt = input.nextInt(); // read first int from user

Outline

9

LinearSearchTest .java

(1 of 2)

// create array and output it

23 24

// repeatedly input an integer; -1 terminates the program

25 26

while ( searchInt != -1 ) {

27

// perform linear search

28 29

position = searchArray.linearSearch( searchInt );

 2005 Pearson Education, Inc. All rights reserved.

30 31 32 33 34 35

if ( position == -1 ) // integer was not found System.out.println( "The integer " + searchInt + " was not found.\n" ); else // integer was found System.out.println( "The integer " + searchInt + " 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 42

Outline

10

LinearSearchTest .java

(2 of 2)

} // end while } // 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

 2005 Pearson Education, Inc. All rights reserved.

11

Efficiency of Linear Search •

Asymptotic Complexity – aka “Big O Notation” – Indicates an abstract notion of run time for an algorithm – In other words, how hard an algorithm has to work to solve a problem.



Definition: f(n) = O(g(n)) means 1. there are positive constants c and k, 2. such that 0 ≤ f(n) ≤ cg(n) for all n ≥ k. 3. the values of c and k must be fixed for the function f and must not depend on n.

 2005 Pearson Education, Inc. All rights reserved.

12

Complexity – Informally, some examples • 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

 2005 Pearson Education, Inc. All rights reserved.

13

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

 2005 Pearson Education, Inc. All rights reserved.

14

Performance Tip 16.1 Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to understand, program, test and debug. Sometimes more sophisticated algorithms are required to achieve usable levels of performance.

 2005 Pearson Education, Inc. All rights reserved.

15

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

 2005 Pearson Education, Inc. All rights reserved.

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;

Outline

16

BinaryArray.java

6 7

public class BinaryArray

8

{

9

private int[] data; // array of values

10

private static Random generator = new Random();

(1 of 3)

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 22 23

Arrays.sort( data ); } // end BinaryArray constructor

 2005 Pearson Education, Inc. All rights reserved.

24 25 26

// perform a binary search on the data public int binarySearch( int searchElement ) {

Store high, low and middle of Outline remaining array to search

27 28

int low = 0; // low end of the search area int high = data.length - 1; // high end of the search area

29 30

int middle = ( low + high + 1 ) / 2; // middle element int location = -1; // return value; -1 if not found Loop

31 32 33

do // loop to search for element {

BinaryArray.java

until key is found or no elements left to search (2 of 3)

34 35

// print remaining elements of array System.out.print( remainingElements( low, high ) );

36 37 38

// output spaces for alignment for ( int i = 0; i < middle; i++ )

39 40

System.out.print( " " ); System.out.println( " * " ); // indicate current middle

41 42 43

// if the element is found at the middle if ( searchElement == data[ middle ] )

44 45

17

If search element is the middle element Return middle element

If search location = middle; // location is the current middle

46 47 48

// middle element is too high else if ( searchElement < data[ middle ] ) high = middle - 1; // eliminate the higher half

49 50 51

else // middle element is too low low = middle + 1; // eliminate the lower half

element is less than middle element

Eliminate higher half Else, eliminate lower half  2005 Pearson Education, Inc. All rights reserved.

52 53 54

middle = ( low + high + 1 ) / 2; // recalculate the middle } while ( ( low 0 && data[ moveItem - 1 ] > insert )

37

{

Move one element to the right

38

// shift element right one slot

39

data[ moveItem ] = data[ moveItem - 1 ];

40

moveItem--;

41

} // end while

Decrement location to insert element

42 43

data[ moveItem ] = insert; // place inserted element

44

printPass( next, moveItem ); // output pass of algorithm

45 46

} // end for } // end method sort

Insert element into sorted place

47

 2005 Pearson Education, Inc. All rights reserved.

48

// print a pass of the algorithm

49

public void printPass( int pass, int index )

50

{

51

Outline

34

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 ] + "

" );

InsertionSort.java

(3 of 4)

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 = 1 ) // if not base case Compute middle { int middle1 = ( low + high ) / 2; // calculate middle of array

34 35 36

int middle2 = middle1 + 1; // calculate next element over

37 38 39 40

System.out.println( "split: System.out.println( " System.out.println( " System.out.println();

41 42 43

// split array in half; sort each half (recursive calls) Recursively sortArray( low, middle1 ); // first half of array

44 45 46 47 48 49 50

// output split step

case Outline

41

of array MergeSort.java

(2 of 5) Compute element one spot to right of middle low, high ) );

" + subarray( " + subarray( low, middle1 ); " + subarray( middle2, high ) );

Recursively sort first half of array

sortArray( middle2, high ); // second half of array

sort second half of array

// merge two sorted arrays after split calls return merge ( low, middle1, middle2, high ); } // end if } // end method split

Merge the two sorted subarrays

 2005 Pearson Education, Inc. All rights reserved.

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

// merge two sorted subarrays into one sorted subarray private void merge( int left, int middle1, int middle2, int right ) Index of element { int leftIndex = left; // index into left subarray

in left Outline array

42

Index of element in right array int rightIndex = middle2; // index into right subarray int combinedIndex = left; // index into temporary working array MergeSort.java Index to place element in combined int[] combined = new int[ data.length ]; // working array array // output two subarrays before merging System.out.println( "merge: " + subarray( left, middle1 Array )to); hold System.out.println( "

(3 of 5)

sorted elements

" + subarray( middle2, right ) );

Loop until reach end of either array

// merge arrays until reaching end of either while ( leftIndex

Suggest Documents