1 2. program construction in java. 2.8 searching

15
1 2. Program Construction in Java

Upload: rodger-benson

Post on 01-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 2. Program Construction in Java. 2.8 Searching

1

2. Program Construction in Java

Page 2: 1 2. Program Construction in Java. 2.8 Searching

2.8 Searching

Page 3: 1 2. Program Construction in Java. 2.8 Searching

3

The need

•Consider such vast databases as media players (iTunes, Spotify) and geographical information systems (GES's, GoogleEarth)

•Getting to a particular track or location quickly is becoming increasingly important.

•We consider two methods of searching...

Page 4: 1 2. Program Construction in Java. 2.8 Searching

4

Linear search

•Basic algorithm: start at one end, pass through each element comparing with the wanted value, stop when they match (return a dummy or sentinel value if not found).

•This is slow with larger arrays, but is simple to program and does not depend on the array being in order.

Page 5: 1 2. Program Construction in Java. 2.8 Searching

5

Linear search

‣ int [] ages = {17,19,15,21,19,23, 14,16};int wanted = input (“Which age are you looking for? “);for (int i=0; i<=7; i++) { if (ages[i] == wanted){ output (“Posn. “ + i); break; } output (“Value not found.“);}

Page 6: 1 2. Program Construction in Java. 2.8 Searching

6

Linear search

‣ String [] channels = {"TF1","FR2","FR3","ARTE","TV5","M6","TLT","BBC1","CNN","TVE"};String wanted = input (“Which channel are you looking for? “);// call the search method:output (“This channel is at position “ + linearSearch (wanted);

//continued...

Page 7: 1 2. Program Construction in Java. 2.8 Searching

7

Linear search

‣ public static int linearSearch (String wanted){ for (int i=0; i<=9; i++) { if (wanted.equals(channels[i]){ return i; } } return -1;}

Page 8: 1 2. Program Construction in Java. 2.8 Searching

8

Binary search

•Basic algorithm: if the array is in order, split it in two, decide whether the wanted value is lower or higher than the middle, then do the same on just that half and continue until the wanted value is found (return a sentinel if not found).

Page 9: 1 2. Program Construction in Java. 2.8 Searching

9

Binary search

•This depends on the array being kept in order (not as uncommon as you might think).

•A binary search will be much faster than a linear one on large arrays.

•A Java implementation of the binary search is available in the project ArrayBinarySearch but will not be examined until we have studied sorting.

Page 10: 1 2. Program Construction in Java. 2.8 Searching

10

Binary search

•Initialise these values:

‣ // value to be returned int result = -1;// start point of list being examined int first = 0; // end point of list being examined int last = 99;// mid point of the list int mid;

Page 11: 1 2. Program Construction in Java. 2.8 Searching

11

Binary search

•Then enter a loop that continues while the first and loast have not collided and while the value has not been found

‣ while ((first <= last) && (result == -1)) {

Page 12: 1 2. Program Construction in Java. 2.8 Searching

12

Binary search

•Pick the mid point and maybe it’s your lucky day!

‣ mid = (first + last) / 2;

‣ if (wanted == numbers [mid]{ result = mid; // get it's position in the array}

Page 13: 1 2. Program Construction in Java. 2.8 Searching

13

Binary search

•But that’s unlikely, so reduce the list by half and try again

‣ else {

‣ if (wanted < numbers[mid]) { // wanted is in lower half last = mid - 1; // adjust last } else { // it must be in the upper half first = mid + 1; // adjust first }}

Page 14: 1 2. Program Construction in Java. 2.8 Searching

14

Binary search

•Keep going around the loop until first > last or wanted is found, then

‣ return result;

Page 15: 1 2. Program Construction in Java. 2.8 Searching

15

Binary search

•[A teaser for HL candidates]

•Since a binary search is very repetitive (split and jump one way, split and jump one way...), it can also be handled using recursion (calling the method from inside itself) [see Recursion, HL only)