Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Search and Sort Asserting Java ©Rick Mercer.

Similar presentations


Presentation on theme: "Chapter 8 Search and Sort Asserting Java ©Rick Mercer."— Presentation transcript:

1 Chapter 8 Search and Sort Asserting Java ©Rick Mercer

2 Outline  Understand how binary search finds elements more quickly than sequential search  Sort array elements  Implement and call static methods  Declare arrays with two and three subscripts  Process data stored in rows and columns

3 Binary Search  Binary search serves the same service as sequential search, but faster. — Especially when there are many array elements  You employ a similar algorithm when you look up a name in a phonebook. while the element is not found and it still may be in the array while the element is not found and it still may be in the array { if the element in the middle of the array is the matches if the element in the middle of the array is the matches store the reference and signal that the element was found store the reference and signal that the element was found else else eliminate the correct half of the array from further search eliminate the correct half of the array from further search }

4 Binary Search  We'll see that binary search can be a more efficient algorithm for searching. — It works only on sorted arrays like this Compare the element in the middle Compare the element in the middle if that's the target, quit and report success if that's the target, quit and report success if the key is smaller, search the array to the left if the key is smaller, search the array to the left otherwise search the array to the right otherwise search the array to the right — This process repeats until we find the target or there is nothing left to search

5 Some preconditions  For Binary Search to work: — The array must be sorted — The indexes referencing the first and last elements must represent the entire range of meaningful elements in the array

6 Binary search a small array  Here is a small array that will be searched over the next few slides. int n = 9; int n = 9; String[] a = new String[n]; String[] a = new String[n]; // Insert elements in natural order (according to // Insert elements in natural order (according to // the compareTo method of the String class // the compareTo method of the String class a[0] = "Bob"; a[0] = "Bob"; a[1] = "Carl"; a[1] = "Carl"; a[2] = "Debbie"; a[2] = "Debbie"; a[3] = "Evan"; a[3] = "Evan"; a[4] = "Froggie"; a[4] = "Froggie"; a[5] = "Gene"; a[5] = "Gene"; a[6] = "Harry"; a[6] = "Harry"; a[7] = "Igor"; a[7] = "Igor"; a[8] = "Jose"; a[8] = "Jose"; // The array is filled to capacity in this example // The array is filled to capacity in this example

7 Initialize other variables  Several assignments to get things going. int first = 0; int first = 0; int last = n - 1; int last = n - 1; String searchString = "Harry"; String searchString = "Harry"; // –1 will mean that the element has not yet been found // –1 will mean that the element has not yet been found int indexInArray = -1; int indexInArray = -1;  The first element to be compared is the one in the middle. int mid = ( first + last ) / 2; int mid = ( first + last ) / 2;  If the middle element matches searchString, stop.  Otherwise move low above mid or high below mid. — This will eliminate half of the elements from the search The next slide shows the binary search algorithm The next slide shows the binary search algorithm

8 Binary Search Algorithm while indexInArray is -1 and there are more array elements to look through { if searchString is equal to name[mid] then if searchString is equal to name[mid] then let indexInArray = mid // array element equaled searchString let indexInArray = mid // array element equaled searchString else if searchString alphabetically precedes name[mid] else if searchString alphabetically precedes name[mid] eliminate mid... last elements from the search eliminate mid... last elements from the search else else eliminate first... mid elements from the search eliminate first... mid elements from the search mid = ( first + last ) / 2; // Compute a new mid for the next iteration mid = ( first + last ) / 2; // Compute a new mid for the next iteration}  Next slide shows mid goes from 4 to (5+8)/2=6

9 Binary Search Harry Bob Carl Froggie Gene Harry Igor Debbie Evan a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] first mid last first mid found last Jose Datareference pass 1 pass 2

10 Binary Search Alice not in array Bob Carl Froggie Gene Harry Igor Debbie Evan a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] first mid last first mid last Jose data reference pass 1 pass 2 pass 3 first mid last pass5 At pass4 (not shown) all integers are 0 to consider the first in the array. However at pass5, last becomes -1. At pass5, indexInArray is still -1, however last is < first to indicate the element was not found. The loop test: while(indexInArray == -1 && first <= last) last first mid ?

11 Binary Search Code  Binary search code: while( indexInArray == -1 && ( first <= last ) ) while( indexInArray == -1 && ( first <= last ) ) { if( searchString.equals( name[mid] ) ) if( searchString.equals( name[mid] ) ) indexInArray = mid; // found indexInArray = mid; // found else if( searchString.compareTo( name[mid] ) < 0 ) else if( searchString.compareTo( name[mid] ) < 0 ) last = mid-1 ; // searchString may be in 1 st half last = mid-1 ; // searchString may be in 1 st half else else first= mid+1; // searchString may be in second first= mid+1; // searchString may be in second mid = ( first + last ) / 2; mid = ( first + last ) / 2; } // end while } // end while

12 How fast is Binary Search?  Best case: 1  Worst case: when target is not in the array  At each pass, the "live" portion of the array is narrowed to half the previous size.  The series proceeds like this: — n, n/2, n/4, n/8,...  Each term in the series represents one comp- arison. How long does it take to get to 1? — This will be the number of comparisons

13 Graph Illustrating Relative growth logarithmic and linear n f(n) searching with sequential search searching with binary search

14 Comparing sequential search to binary search Rates of growth and logarithmic functions

15 Sorting  Sorting: the process of arranging array elements into ascending or descending order: — Ascending (where x is an array object): x[0] <= x[1] <= x[2] <=... <= x[n-2] <= x[n-1] x[0] <= x[1] <= x[2] <=... <= x[n-2] <= x[n-1] — Descending: x[0] >= x[1] >= x[2] >=... >= x[n-2] >= x[n-1] x[0] >= x[1] >= x[2] >=... >= x[n-2] >= x[n-1] — Here's the data used in the next few slides:

16 Swap smallest with the first // Swap the smallest element with the first top = 0 smallestIndex = top for j ranging from top+1 through n – 1 { if test[ j ] < test[ smallestIndex ] then if test[ j ] < test[ smallestIndex ] then smallestIndex = j smallestIndex = j} // Question: What is smallestIndex now __________? swap test[ smallestIndex ] with test[ top ]

17 Selection sort algorithm  Now we can sort the entire array by changing top from 0 to n-2 with this loop. for (top = 0; top < n-1; top++) for each subarray, move the smallest to the top for each subarray, move the smallest to the top  The top moves down one array position each time the smallest is placed on top.  Eventually, the array is sorted.

18 Trace selection sort

19 Selection sort runtimes Actual observed data 1 10 20 3 0 40 in thousands


Download ppt "Chapter 8 Search and Sort Asserting Java ©Rick Mercer."

Similar presentations


Ads by Google