22
Chapter 8 Search and Sort © Rick Mercer

Chapter 8 Search and Sort - University of Arizona

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 8 Search and Sort - University of Arizona

Chapter 8Search and Sort

©Rick Mercer

Page 2: Chapter 8 Search and Sort - University of Arizona

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 phonebookwhile the element is not found and it still may be in the array {

if the element in the middle of the array is the matchesstore the reference and signal that the element was found

elseeliminate correct half of the array from further search

}

Page 3: Chapter 8 Search and Sort - University of Arizona

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– if that's the target, quit and report success– if the key is smaller, search the array to the left– otherwise search the array to the right

• This process repeats until we find the target or there is nothing left to search

Page 4: Chapter 8 Search and Sort - University of Arizona

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

Page 5: Chapter 8 Search and Sort - University of Arizona

Binary search a small array

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

Page 6: Chapter 8 Search and Sort - University of Arizona

Initialize other variables• Several assignments to get things going

int first = 0;int last = n - 1;String searchString = "Harry";// –1 means the element has not yet been foundint indexInArray = -1;

• The first element to be compared is the one in the middle.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

Page 7: Chapter 8 Search and Sort - University of Arizona

Binary Search Algorithmwhile indexInArray is -1 and there are more elements {

if searchString is equal to name[mid] then let indexInArray = mid // array element equaled searchString

else if searchString alphabetically precedes name[mid] eliminate mid . . . last elements from the search

else eliminate first . . . mid elements from the search

mid = ( first + last ) / 2; // Compute a new mid for the next iteration}

– Next slide shows mid goes from 4 to (5+8)/2=6

Page 8: Chapter 8 Search and Sort - University of Arizona

Binary Search Harry

BobCarl

FroggieGeneHarryIgor

DebbieEvan

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

first

mid

last

first

mid found

lastJose

Data reference pass 1 pass 2

Page 9: Chapter 8 Search and Sort - University of Arizona

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

?

Page 10: Chapter 8 Search and Sort - University of Arizona

Binary Search Code

while(indexInArray == -1 && (first <= last)) {

if( searchString.equals(a[mid] ) )

indexInArray = mid; // found

else if( searchString.compareTo(a[mid] ) < 0 )

last = mid-1 ; // may be in 1st half

else

first= mid+1; // may be in second

mid = ( first + last ) / 2;

}

System.out.println("Index of " + searchString +

": " + indexInArray);

Page 11: Chapter 8 Search and Sort - University of Arizona

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 is 1 comparison• How long does it take to get to 1?

–This will be the number of comparisons

Page 12: Chapter 8 Search and Sort - University of Arizona

Graph Illustrating Relative growth logarithmic and linear

n

f(n) searching with sequential search

searching with binary search

Page 13: Chapter 8 Search and Sort - University of Arizona

Comparing sequential search to binary search

Power of 2 n log2n

24 16 4

28 128 8

212 4,096 12

224 16,777,216 24

Rates of growth and logarithmic functions

Page 14: Chapter 8 Search and Sort - University of Arizona

Other logarithm examples

wThe guessing game:¾ Guess a number from 1 to 100

• try the middle, you could be right• if it is too high

– check near middle of 1..49• if it is too low

– check near middle of 51..100

¾ Should find the answer in a maximum of 7 tries• If 1..250, a maximum of 2c >= 250, c == 8• If 1..500, a maximum of 2c >= 500, c == 9• If 1..1000, a maximum of 2c >= 1000, c == 10

Page 15: Chapter 8 Search and Sort - University of Arizona

Logarithmic Explosion

wAssuming an infinitely large piece of paper that can be cut in half, layered, and cut in half again as often as you wish.

¾ How many times do you need to cut and layer until paper thickness reaches the moon?

¾ Assumptions• paper is 0.002 inches thick• distance to moon is 240,000 miles

– 240,000 * 5,280 feet per mile * 12 inches per foot = 152,060,000,000 inches to the moon

Page 16: Chapter 8 Search and Sort - University of Arizona

Examples of Logarithmic Explosion

wThe number of bits required to store a binary number is logarithmic add 1 bit to get much larger ints

¾ 8 bits stored 256 values log2256 = 8¾ log 2,147,483,648 = 31

wThe inventor of chess asked the Emperor to be paid like this:

¾ 1 grain of rice on the first square, 2 on the next, double grains on each successive square 263

Page 17: Chapter 8 Search and Sort - University of Arizona

Compare Sequential and Binary Search

wOutput from CompareSearches.java (1995)Search for 20000 objects

Binary Search#Comparisons: 267248

Average: 13Run time: 20ms

Sequential Search#Comparisons: 200010000

Average: 10000Run time: 9930ms

Difference in comparisons : 199742752Difference in milliseconds: 9910

0

200

400

600

800

1000

1200

0 200 400 600 800 1000

Seconds 2013

Page 18: Chapter 8 Search and Sort - University of Arizona

Sorting an Array

Page 19: Chapter 8 Search and Sort - University of Arizona

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]

– Descending:x[0] >= x[1] >= x[2] >= ... >= x[n-2] >= x[n-1]

– Here's the data used in the next few slides:Array Unsorted Sorted

test[0] 76 62 test[1] 74 74 test[2] 100 76 test[3] 62 89 test[4] 89 100

Page 20: Chapter 8 Search and Sort - University of Arizona

Swap smallest with the first// Swap the smallest element with the firsttop = 0smallestIndex = topfor i ranging from top+1 through n – 1 {

if test[ i ] < test[ smallestIndex ] thensmallestIndex = i

}// Question: What is smallestIndex now __________?swap test[ smallestIndex ] with test[ top ]

Page 21: Chapter 8 Search and Sort - University of Arizona

Selection sort algorithm

• Now we can sort the entire array by changing top from 0 to n-2 with this loopfor (top = 0; top < n-1; 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

Page 22: Chapter 8 Search and Sort - University of Arizona

#Time required to sort an array of size n

0

100

200

300

400

n

Seconds

Selection sort runtimesActual observed data on a slow pc

1 10 20 3 0 40 in thousands