25 march 2013birkbeck college, u. london1 introduction to programming lecturer: steve maybank...
TRANSCRIPT
25 March 2013 Birkbeck College, U. London 1
Introduction to Programming
Lecturer: Steve Maybank
Department of Computer Science and Information Systems
[email protected] 2013
Week 11: Examples of Algorithms
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 2013 Birkbeck College, U. London 2
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 2013 Birkbeck College, U. London 3
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 2013 Birkbeck College, U. London 4
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 2013 Birkbeck College, U. London 5
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 2013 Birkbeck College, U. London 6
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 2013 Birkbeck College, U. London 7
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 2013 Birkbeck College, U. London 8
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 2013 Birkbeck College, U. London 9
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 2013 Birkbeck College, U. London 10
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 2013 Birkbeck College, U. London 11
Appointments
25 March 2013 Birkbeck College, U. London 12
timea1 a2 a3 a4
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?
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 2013 Birkbeck College, U. London 13
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 2013 Birkbeck College, U. London 14
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 2013 Birkbeck College, U. London 15
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 2013 Birkbeck College, U. London 16
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 2013 Birkbeck College, U. London 17
The Method linearSearchpublic 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 2013 Birkbeck College, U. London 18
The Method linearSearch2public 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 2013 Birkbeck College, U. London 19
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 2013 Birkbeck College, U. London 20
Strategy for Binary Search
25 March 2013 Birkbeck College, U. London 21
1 3 7 8 11 12 20
low high
Mark out a section of the array data using indices low, highFind an index i between low and highCompare data[i] with e and update low, high accordingly
i
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 2013 Birkbeck College, U. London 22
The Method binarySearchpublic 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 halfelse{high = pos-1;} // look in first half
}return –1;
}
25 March 2013 Birkbeck College, U. London 23
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 2013 Birkbeck College, U. London 24