Download presentation

Presentation is loading. Please wait.

Published byKatelyn Stack Modified over 3 years ago

1
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2013 Week 11: Examples of Algorithms

2
JavaLab 9, Ex. 1 (1) Write a method public static int[] reverseArray(int[] data) such that reverseArray returns the reverse of the array data. Call reverseArray from main. 25 March 2013Birkbeck College, U. London2

3
JavaLab 9, Ex. 1 (2) import java.util.Arrays; /** * This program reverses the order of the elements in an array * @author S.J. Maybank * @version 25 March 2013 */ public class ReverseArray { // main defined here // reverseArray defined here } 25 March 2013Birkbeck College, U. London3

4
JavaLab 9, Ex. 1 (3) public static void main(String[] args) { int[] data1 = {1, 2, 3, 4, 5}; int[] data2 = {}; int[] data3 = {1, 2, 3, 4}; int[] data1R = reverseArray(data1); int[] data2R = reverseArray(data2); int[] data3R = reverseArray(data3); System.out.println("Original array: "+Arrays.toString(data1)); System.out.println("Reversed array: "+Arrays.toString(data1R)); // data2, dataR and data3, data3R printed out similarly } 25 March 2013Birkbeck College, U. London4

5
JavaLab 9, Ex. 1 (4) public static int[] reverseArray(int[] data) { int[] dataReversed = new int[data.length]; for(int i = 0; i < data.length; i++) { dataReversed[data.length-1-i] = data[i]; } return dataReversed; } 25 March 2013Birkbeck College, U. London5

6
JavaLab 9, Ex. 2 (1) /** * A program to apply certain methods to an array of integers. * @author S.J. Maybank * @version 25 March 2013 */ public class ArrayMethods { // main // printArray // productElements // numberNegativeElements } 25 March 2013Birkbeck College, U. London6

7
JavaLab 9, Ex. 2 (2) public static void main(String[] args) { int[] data = {1, 2, 3, -1, -3}; printArray(data); System.out.println("Product elements: "+productElements(data)); System.out.print("Number of elements strictly less than 0: "); System.out.println(""+numberNegativeElements(data)); } 25 March 2013Birkbeck College, U. London7

8
JavaLab 8, Ex. 2 (3) public static void printArray(int[] data) { for(int i = 0; i < data.length; i++) { System.out.print(data[i]); if (i < data.length-1) { System.out.print(" "); } System.out.println(); } 25 March 2013Birkbeck College, U. London8

9
JavaLab 9, Ex. 2 (4) public static int productElements(int[] data) { int product = 1; for(int i = 0; i < data.length; i++) { product *= data[i]; } return product; } 25 March 2013Birkbeck College, U. London9

10
JavaLab 9. Ex. 2 (5) public static int numberNegativeElements(int[] data) { int n = 0; for(int i = 0; i < data.length; i++) { if(data[i] < 0) { ++n; } return n; } 25 March 2013Birkbeck College, U. London10

11
Overview Test to see if two appointments overlap (JFE, R3.11) Linear search of an array (JFE, Section 6.3.5) Binary search of an array (JFE, end of Section 6.3) 25 March 2013Birkbeck College, U. London11

12
Appointments 25 March 2013Birkbeck College, U. London12 time a1a2a3a4 Non-overlapping appointments: [a1, a2] and [a3, a4] Overlapping appointments: [a1, a3] and [a2, a4] [a1, a4] and [a2, a3] How to decide when two appointments overlap?

13
Overlapping Appointments Let the appointments be [s1, e1] and [s2, e2]. Let s be the latest start and let e be the earliest end. If s > e, then one appointment begins after the other has finished. Conversely, let t be any time such that s t e. The time t is in [s1, e1] because s1 s t e e1 The time t is in [s2, e2] because s2 s t e e2 25 March 2013Birkbeck College, U. London13

14
Pseudo Code for overLappingAppointments Inputs: times s1, e1 and s2, e2 for two appointments. Output: true if the appointments overlap and false otherwise if (s1 > s2) s = s1 else s = s2 if (e1 < e2) e = e1 else e = e2 if (s < e) return true else return false 25 March 2013Birkbeck College, U. London14

15
The Method overLappingAppointments public static boolean overLappingAppointments(int s1, int e1, int s2, int e2) { int s, e; if(s1 > s2){s = s1;} else {s = s2;} if(e1 < e2){e = e1;} else {e = e2;} return s < e; } 25 March 2013Birkbeck College, U. London15

16
Inputs and Output for Linear Search Inputs: 1D integer array data and an integer e. Output: if data contains e then an integer pos such that data[pos] == e, otherwise –1. 25 March 2013Birkbeck College, U. London16

17
Pseudo Code for Linear Search 1. Step through the valid indices pos for the array data, if data[pos] == e, then return pos. 2. If all the valid indices have been checked without finding e, then return -1 25 March 2013Birkbeck College, U. London17

18
The Method linearSearch public static int linearSearch(int[] data, int e) { int pos = 0; while (pos < data.length) { if(data[pos] == e){return pos;} ++pos; } return –1; } 25 March 2013Birkbeck College, U. London18

19
The Method linearSearch2 public static int linearSearch2(int[] data, int e) { int pos = data.length-1; while (pos>=0) { if(data[pos] == e){return pos;} pos--; } return –1; } 25 March 2013Birkbeck College, U. London19

20
Inputs and Output for Binary Search Inputs: 1D sorted integer array data and an integer e. Output: if data contains e then an integer pos such that data[pos] == e, otherwise –1. 25 March 2013Birkbeck College, U. London20

21
Strategy for Binary Search 25 March 2013Birkbeck College, U. London21 1378111220 low high Mark out a section of the array data using indices low, high Find an index i between low and high Compare data[i] with e and update low, high accordingly i

22
Pseudo Code for Binary Search Set low equal to the least index for data. Set high equal to the largest index for data. while (low <= high) { Find an index pos between low and high. if (data[pos] == e) then return pos. if (data[pos] < e) then high = pos-1. if (data[pos] > e) then low = pos+1. } return –1. 25 March 2013Birkbeck College, U. London22

23
The Method binarySearch public static int binarySearch(int[] data, int e) { int low = 0, high = data.length-1, pos = 0; while(low <= high) { pos = (low+high)/2; if (data[pos] == e){return pos;} if (data[pos] < e) {low = pos+1;} // look in second half else {high = pos-1;} // look in first half } return –1; } 25 March 2013Birkbeck College, U. London23

24
Example of Binary Search Inputs: data = {1, 3, 5, 7, 9, 11}, e = 6. low = 0, high = 5, pos = 2. data[pos] < e, thus low = pos+1 = 3. low = 3, high = 5, pos = 4. data[pos] > e, thus high = pos-1 = 3. low = 3, high = 3, pos = 3. data[pos] > e, thus high = pos-1 = 2. low = 3, high =2 (low <= high) == false, search terminates. 25 March 2013Birkbeck College, U. London24

Similar presentations

OK

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 1 Searching When we maintain a collection of data,

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 1 Searching When we maintain a collection of data,

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on physical change and chemical change Ppt on fact and opinion Ppt on unity in diversity india Ppt on maths for class 3 Ppt on united nations agencies Ppt on construction company profile Ppt on fire management Ppt on ancient 7 wonders of the world Ppt on resistance temperature detector equation Ppt on business communication skills