35
F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson

F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson

Embed Size (px)

Citation preview

F27SA1 Software Development 19. Java Programming 8

Greg Michaelson

Searching

• to find a value in an unordered array– start at first element– check each element in turn until last element

• linear search• for array length N– worst case

• element we want is last so have to check N elements– average case

• if each element is equally likely to be the one we want then have to check N/2 on average

Searching

• number of comparisons significant with:– large amounts of data– large data items

• can search far more quickly if array is ordered• can use order to guide where to look• e.g. binary search – more soon...• how to order data?

Ordering data

• given unordered sequence of data• put in array in ascending order• while more data– get next data– search for first element > next data– move all elements down a place in array– put next data into array

Ordering data

0

210 3

data

dataNo 0

free

input: 5 11 7 19 6 8 ...

4

0

210 3

data

dataNo 1

free

input: 11 7 19 6 8 ...

4

5

Ordering data

0

210 3

data

dataNo 2

free

input: 7 19 6 8 ...

4

0

210 3

data

dataNo 3

input: 19 6 8 ...

4

5

5 11

free117

Ordering data

4

210 3

data

dataNo 4

free

input: 6 8 ...

4

0

210 3

data

dataNo 5

input: 8 ...

4

5

5 11

free117

7 19

196

Ordering data

• use linear search to find position for next dataint i;for(i=0;i<dataNo;i++) if(data[i]>v) break;NBa)break == end enclosing loopb)want to know value of i after loop so define

before loop

Ordering data

• suppose next data should go into data[i]• move all elements down one place in array:– data[dataNo] = data[dataNo-1]– data[dataNo-1] = data[dataNo-2]

...– data[i+1] = data[i]

for(int j=dataNo;j>i;j--)

data[j] = data[j-1]

Ordering data• e.g. insert 6 withdata[0] == 2data[1] == 7data[2] == 11data[3] == 19dataNo == 4• 6 goes into data[1] so i == 1• so j from 4 to 2j == 4 data[4] data[3] == 19j == 3 data[3] data[2] == 11j == 2 data[2] data[1] == 7

Ordering dataimport java.io.*;import java.util.*;

class Insert{ int [] data; int dataNo;

Insert(int n) { data = new int[n]; dataNo = 0; }

void showData() { for(int i=0;i<dataNo;i++) System.out.print(i+":"+data[i]+" "); System.out.println(); }

Ordering data void insert(int v) { if(dataNo==data.length) { System.out.println("No more space."); System.exit(0); } int i; for(i=0;i<dataNo;i++) /* find position for v */ if(data[i]>v) break; for(int j=dataNo;j>i;j--) /* move all after v down */ data[j] = data[j-1]; data[i] = v; /* add v */ dataNo++; }

}

Ordering data void getData(Scanner s) { while(s.hasNext()) insert(s.nextInt()); }}

class TestInsert{ public static void main(String [] argv) throws FileNotFoundException { Insert ins = new Insert(10); ins.getData(new Scanner(new File(argv[0]))); ins.showData(); }}

Binary Search

• divide & conquer• start in middle of array• if element we want comes before one in middle – try left half

• if element we want comes after one in middle – try right half

• every time, halve the search area• eventually search area has only 1 element

Binary Search• e.g. find 27 in: 0 1 2 3 4 5 6 711 19 23 25 27 29 31 34

middle is (0+7)/2 == 3

0 1 2 3 4 5 6 711 19 23 25 27 29 31 34

27 > 25 so look between 4 & 7

Binary Search

0 1 2 3 4 5 6 711 19 23 25 27 29 31 34

middle is (4+7)/2 == 5

0 1 2 3 4 5 6 711 19 23 25 27 29 31 34

27 < 29 so look between 4 & 4

Binary Search 0 1 2 3 4 5 6 711 19 23 25 27 29 31 34

middle is (4+4)/2 == 4

0 1 2 3 4 5 6 711 19 23 25 27 29 31 34

• 3 comparisons • 5 for linear search

Binary Search

• worst case number of checks– how often we can divide N by 2?

• if XY=Z then logX Z = Y – Y is number of times can divide Z by X

• halving so dividing by 2 so log2 N– 8 = 2*2*2 = 23 • so log2 8 == 3

– 32 = 2*2*2*2*2 = 25 • so log2 32 == 5

Binary Search

• log2 N always far smaller than N

• for 4GB = 232

–only 32 checks in worst case!

N 2 4 8 16 32 64 128 256 512

log2 N 1 2 3 4 5 6 7 8 9

Binary Search

• maintain indices to left & right• start with:– left = 0 – right = N

• start position = (left + right) / 2

Binary Search

• while left <= right– if value at position is required value• found required value

– if value at position < required value• required must be in right half so left = position + 1

– if value at position > required value• required must be in left half so right = position – 1

– position = (left + right) / 2• left > right so failed to find required value

Binary Search

$ java TestBinary numbers.txt

0:1 1:22 2:24 3:31 4:46 5:51 6:67 7:75 8:94

Enter value> 67

67 is at position 6

0:1 1:22 2:24 3:31 4:46 5:51 6:67 7:75 8:94

Enter value> 4

4 not found

0:1 1:22 2:24 3:31 4:46 5:51 6:67 7:75 8:94

Enter value>

Binary Search

class Binary

{ same fields & methods as Insert

int pos;

int getPos()

{ return pos; }

Binary Search boolean search(int v)

{ int left = 0;

int right = data.length-1;

pos = (left+right)/2;

while(left<=right)

{ if(a[pos]==v)return true;

if(a[pos]<v)

left = pos+1;

else

right = pos-1;

pos = (left+right)/2;

}

return false;

}

}

Binary Searchclass TestBinary

{ same main as Insert Scanner s = new Scanner(System.in);

int v;

while(true)

{ b.showData(); System.out.print("Enter value> ");

v = s.nextInt();

if(b.search(v))

System.out.println

(v+" is at position "+b.getPos());

else

System.out.println(v+" not found");

}

}

}

Writing to files

FileOutputStream(File)• opens a byte output stream for the File• if File already exists then it is deleted• File is created• low-level output stream• not directly suitable for characters/text

Writing to files

PrintWriter(FileOutputStream)• enables character/text output to the FileOutputStream with:– print– println

close()• closes PrintWriter i.e. closes associated file• good practice to close files explicitly• at end of program, system will close files

Example: output ordered data back to file

• method– send each element of array to PrintWriter – on separate line

• main:– create File object for command line argument– create Scanner for File– input from Scanner– close Scanner

Example: output ordered data back to file

• create PrintWriter for FileOutputStream for File– deletes old file associated with File– creates new file associated with File• with same name as old file

• output data to PrintWriter• close PrintWriter

Example: output data back to file class WriteData

{ same fields & methods as Binary ...

void writeData(PrintWriter p)

{ for(int i=0;i<dataNo;i++)

p.println(data[i]);

}

}

Example: output ordered data back to file

class TestWriteData

{ public static void main(String [] argv)

throws FileNotFoundException

{ WriteData b = new WriteData(10);

File f = new File(argv[0]);

Scanner s = new Scanner(f);

b.getData(s);

s.close();

PrintWriter p =

new PrintWriter(new FileOutputStream(f));

b.writeData(p);

p.close();

}

}

Example: file to file copy$ cat story3.txtOnce upon a time, a small boy and his mother set off to go to the beach. At the station, the mother bought a packet of banana crisps for a snack....$ java TestCopy story3.txt story4.txt$ cat story4.txtOnce upon a time, a small boy and hismother set off to go to the beach. At the station, the mother bought a packet of banana crisps for a snack....

Example: file to file copy

• create a Scanner for the file to be copied• create a PrintWriter for the copy file• so long as there is more input in the file to be

copied– get next line from the file to be copied and send it

to the copy file

• close the copy file

Example: file to file copyimport java.io.*;

import java.util.*;

class Copy

{ Copy(){}

void doCopy(Scanner fromFile,PrintWriter toFile)

{ while(fromFile.hasNext())

toFile.println(fromFile.nextLine());

toFile.close();

}

}

Example: file to file copyclass TestCopy

{ public static void main(String [] argv)

throws FileNotFoundException

{ Scanner fromFile = new Scanner(new File(argv[0]));

PrintWriter toFile =

new PrintWriter(

new FileOutputStream(new File(argv[1])));

Copy c = new Copy();

c.doCopy(fromFile,toFile);

}

}