Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson."— Presentation transcript:

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

2 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

3 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?

4 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

5 Ordering data 0 2103 data dataNo 0 free input: 5 11 7 19 6 8... 4 0 2103 data dataNo 1 free input: 11 7 19 6 8... 4 5

6 Ordering data 0 2103 data dataNo 2 free input: 7 19 6 8... 4 0 2103 data dataNo 3 input: 19 6 8... 4 5 511 free 117

7 Ordering data 4 2103 data dataNo 4 free input: 6 8... 4 0 2103 data dataNo 5 input: 8... 4 5 511 free 117 719 6

8 Ordering data use linear search to find position for next data int i; for(i=0;i<dataNo;i++) if(data[i]>v) break; NB a)break == end enclosing loop b)want to know value of i after loop so define before loop

9 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]

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

11 Ordering data import 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(); }

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

13 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(); }

14 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

15 Binary Search e.g. find 27 in: 0 1 2 3 4 5 6 7 11 19 23 25 27 29 31 34 middle is (0+7)/2 == 3 0 1 2 3 4 5 6 7 11 19 23 25 27 29 31 34 27 > 25 so look between 4 & 7

16 Binary Search 0 1 2 3 4 5 6 7 11 19 23 25 27 29 31 34 middle is (4+7)/2 == 5 0 1 2 3 4 5 6 7 11 19 23 25 27 29 31 34 27 < 29 so look between 4 & 4

17 Binary Search 0 1 2 3 4 5 6 7 11 19 23 25 27 29 31 34 middle is (4+4)/2 == 4 0 1 2 3 4 5 6 7 11 19 23 25 27 29 31 34 3 comparisons 5 for linear search

18 Binary Search worst case number of checks – how often we can divide N by 2? if X Y =Z then log X Z = Y – Y is number of times can divide Z by X halving so dividing by 2 so log 2 N – 8 = 2*2*2 = 2 3 so log 2 8 == 3 – 32 = 2*2*2*2*2 = 2 5 so log 2 32 == 5

19 Binary Search log 2 N always far smaller than N for 4GB = 2 32 – only 32 checks in worst case! N248163264128256512 log 2 N123456789

20 Binary Search maintain indices to left & right start with: – left = 0 – right = N start position = (left + right) / 2

21 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

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

23 Binary Search class Binary { same fields & methods as Insert int pos; int getPos() { return pos; }

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

25 Binary Search class 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"); }

26 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

27 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

28 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

29 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

30 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]); }

31 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(); }

32 Example: file to file copy $ cat story3.txt Once 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.txt Once 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....

33 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

34 Example: file to file copy import java.io.*; import java.util.*; class Copy { Copy(){} void doCopy(Scanner fromFile,PrintWriter toFile) { while(fromFile.hasNext()) toFile.println(fromFile.nextLine()); toFile.close(); }

35 Example: file to file copy class 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); }


Download ppt "F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson."

Similar presentations


Ads by Google