2. program construction in java

Post on 12-Jan-2016

20 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

2. Program Construction in Java. 2.8 Searching. 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. - PowerPoint PPT Presentation

TRANSCRIPT

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...

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.

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.“);}

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...

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;}

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).

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.

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;

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)) {

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}

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 }}

14

Binary search

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

‣ return result;

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)

top related