Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.

Similar presentations


Presentation on theme: "Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting."— Presentation transcript:

1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting

2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Searching Searching is the process of finding a target element within a group of items called the search pool The target may or may not be in the search pool We want to perform the search efficiently, minimizing the number of comparisons Let's look at two classic searching approaches: linear search and binary search

3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Polymorphism in Searching Recall that an class that implements the Comparable interface defines a compareTo method to determine the relative order of its objects We can use polymorphism to develop a generic search for any set of Comparable objects The searching method accepts as a parameter an array of Comparable objects That way, one method can be used to search target from a group of People, or Books, or whatever

4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Linear Search A linear search begins at one end of a list and examines each element in turn Eventually, either the item is found or the end of the list is encountered See PhoneList2.java PhoneList2.java See Searching.java, specifically the linearSearch method Searching.java

5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Search A binary search assumes the list of items in the search pool is sorted It eliminates a large part of the search pool with a single comparison A binary search first examines the middle element of the list -- if it matches the target, the search is over If it doesn't, only one half of the remaining elements need be searched Since they are sorted, the target can only be in one half of the other number 512172338 4477 012345678 8490

6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Successful Search - 1 0808 #1 high low lowhighmid mid 4 search( 44 ) 512172338 4477 012345678 8490 38 < 44 low = mid+1 = 5

7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Successful Search - 2 search( 44 ) lowhighmid 0808 #1 highlow 5858 #2 512172338 4477 012345678 8490 44 < 77 high = mid-1=5 mid 6 4

8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Successful Search - 3 search( 44 ) lowhighmid 0808 #1 5858 #2 highlow5 #3 512172338 4477 012345678 8490 44 == 44 mid 5 Successful Search!! 4 6

9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Unsuccessful Search - 1 38 < 45 low = mid+1 = 5 lowhighmid 0808 #1 high low mid 4 512172338 4477 012345678 8490 search( 45 )

10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Unsuccessful Search - 2 45 < 77 high = mid-1=5 highlow 5858 #2 512172338 4477 012345678 8490 search( 45 ) lowhighmid 0808 #1 4 mid 6

11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Unsuccessful Search - 3 44 < 45 highlow5 #3 low = mid+1 = 6 search( 45 ) lowhighmid 0808 #1 4 5858 #2 mid 5 512172338 4477 012345678 8490 6

12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Sequence of Unsuccessful Search - 4 Unsuccessful Search low > high no more elements to search 512172338 4477 012345678 8490 search( 45 ) lowhighmid 0808 #1 4 5858 #2 65 #3 5 highlow 6565 #4

13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Binary Search Routine public int binarySearch ( int[] number, int searchValue ) { int low = 0, high = number.length - 1, mid= (low + high) / 2; while ( low <= high && number[mid] != searchValue ) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if ( low > high) { mid = NOT_FOUND; } return mid; }

14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Binary Search Performance Successful Search –Best Case – 1 comparison –Worst Case – log 2 N comparisons Unsuccessful Search –Best Case =Worst Case – log 2 N comparisons Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves.

15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2015-12-19 Comparing N and log 2 N Performance Array SizeLinear – NBinary – log 2 N

16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Search Another Polymorphism Implementation of Binary Search See PhoneList2.java PhoneList2.java See Searching.java, specifically the binarySearch method Searching.java

17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –sorting a list of test scores in ascending numeric order –sorting a list of people alphabetically by last name There are many algorithms, which vary in efficiency, for sorting a list of items We will examine two specific algorithms: –Selection Sort –Insertion Sort

18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Selection Sort The approach of Selection Sort: –select a value and put it in its final place into the list –repeat for all other values In more detail: –find the smallest value in the list –switch it with the value in the first position –find the next smallest value in the list –switch it with the value in the second position –repeat until all values are in their proper places

19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Selection Sort An example: original: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9 Each time, the smallest remaining value is found and exchanged with the element in the "next" position to be filled

20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Swapping The processing of the selection sort algorithm includes the swapping of two values Swapping requires three assignment statements and a temporary storage location: temp = first; first = second; second = temp;

21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Selection Sort As we did with searching, we'll implement the sorting with polymorphic Comparable parameters The sorting method doesn't "care" what it is sorting, it just needs to be able to call the compareTo method That is guaranteed by using Comparable as the parameter type Also, this way each class decides for itself what it means for one object to be less than another See Sorting.java, specifically the selectionSort method Sorting.java See PhoneList.java Contact.java PhoneList.java Contact.java

22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Insertion Sort The approach of Insertion Sort: –pick any item and insert it into its proper place in a sorted sublist –repeat until all items have been inserted In more detail: –consider the first item to be a sorted sublist (of one item) –insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition –insert the third item into the sorted sublist (of two items), shifting items as necessary –repeat until all values are inserted into their proper positions

23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Insertion Sort An example: original: 3 9 6 1 2 insert 9: 3 9 6 1 2 insert 6: 3 6 9 1 2 insert 1: 1 3 6 9 2 insert 2: 1 2 3 6 9 See Sorting.java, specifically the insertionSort method Sorting.java

24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comparing Sorts The Selection and Insertion sort algorithms are similar in efficiency They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list Approximately n 2 number of comparisons are made to sort a list of size n We therefore say that these sorts are of order n 2 Other sorts are more efficient: order n log 2 n

25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exercise Pp9.4 Rewrite the Sorting class so that both sorting algorithms put the values in descending order. Create a driver class with a main method to exercise the modifications.


Download ppt "Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting."

Similar presentations


Ads by Google