25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Zabin Visram Room CS115 CS126 Searching
Research Methods Lecturer: Steve Maybank
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Department of Computer Science and Information Systems Autumn 2013 Preliminary.
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Introduction to Computer Systems
15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Introduction to Programming
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
25 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Data Structures ADT List
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Classes and objects Learning objectives By the end of this lecture you should be able to: explain the meaning of the term object-oriented; explain the.
Computer Programming Lab(7).
Introduction to Computer Systems
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
CS110 Programming Language I
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
11 November 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 Various Methods of Populating Arrays Randomly generated integers.
Computer Programming Lab 8.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Searching Arrays Linear search Binary search small arrays
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 8 ARRAYS Continued
Introduction to Computer Systems
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Introduction to Computer Systems
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Data Structures Arrays and Lists Part 2 More List Operations.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
CSE 1030: Implementing Non-Static Features
searching Concept: Linear search Binary search
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 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 2013Birkbeck College, U. London2

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

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

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

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

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

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

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

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

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

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?

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

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

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

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

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 March 2013Birkbeck College, U. London17

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

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

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

Strategy for Binary Search 25 March 2013Birkbeck College, U. London 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

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

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

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