Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Similar documents
Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

The return Statement

1 class Lecture6 { 2 3 "Methods" // keywords: 8 return. Zheng-Liang Lu Java Programming 186 / 244

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

1 class Lecture6 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 10 / Zheng-Liang Lu Java Programming 185 / 248

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Exercise. Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram.

Recursion 1. Recursion is the process of defining something in terms of itself.

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University

Example. Password generator

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory.

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order).

Example: Fibonacci Numbers

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. ˆ The break statement exits a for or while loop completely.

Lecture 5: Methods CS2301

Pace University. Fundamental Concepts of CS121 1

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Nested Loops. A loop can be nested inside another loop.

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Example: Monte Carlo Simulation 1

CS-201 Introduction to Programming with Java

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Object-Based Programming. Programming with Objects

Lecture #8-10 Arrays

( &% class MyClass { }

Recursion. Chapter 5

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Arithmetic Compound Assignment Operators

Introduction to Programming (Java) 4/12

CS313D: ADVANCED PROGRAMMING LANGUAGE

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Lecture 04 FUNCTIONS AND ARRAYS

Functions. Systems Programming Concepts

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Exercise (Revisited)

CS171:Introduction to Computer Science II

Zheng-Liang Lu Java Programming 45 / 79

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Unit #3: Recursion, Induction, and Loop Invariants

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Methods. Bok, Jong Soon

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Lecture #6-7 Methods

switch-case Statements

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

CS1150 Principles of Computer Science Methods

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

ESC101 : Fundamental of Computing

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

CS1150 Principles of Computer Science Methods

CS110D: PROGRAMMING LANGUAGE I

Unit #2: Recursion, Induction, and Loop Invariants

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Advanced Computer Programming

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

EK131 E5 Introduction to Engineering

JVM (java) compiler. A Java program is either a library of static methods (functions) or a data type definition

1 Lexical Considerations

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

G Programming Languages - Fall 2012

Chapter 3 - Functions

We would like guidance on how to write our programs beyond simply knowing correlations between inputs and outputs.

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

A function that invokes itself is said to

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Methods and Functions

CS110: PROGRAMMING LANGUAGE I

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Programming II (CS300)

Introduction to Computer Science Unit 2. Notes

Transcription:

Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A; // shallow copy; you don t have a new array 4... However, this is impossible to make two distinct arrays. Recall that the array variables are simply references to the arrays in the heap. Zheng-Liang Lu Java Programming 173 / 216

Moreover, all the reference variables share this property! For example, Zheng-Liang Lu Java Programming 174 / 216

Use a loop to copy individual elements one by one. 1... 2 int[] A = {2, 1, 3, 5, 10}; 3 int[] B = new int[a.length]; 4 // deep copy 5 for (int i = 0; i < A.length; ++i) { 6 B[i] = A[i]; 7 } 8... Alternatively, you may use the arraycopy method in the System class. 1... 2 int[] A = {2, 1, 3, 5, 10}; 3 int[] B = new int[a.length]; 4 System.arraycopy(A, 0, B, 0, A.length); 5... Zheng-Liang Lu Java Programming 175 / 216

for-each Loops 1 A for-each loop is designed to iterate over a collection of objects, such as arrays and other data structures, in strictly sequential fashion, from start to finish. For example, 1... 2 T[] A = {...}; // assume some T type array 3 for (T element: A) { 4 // body 5 } 6... Note that the type T should be compatible to the element type of A. 1 Beginning with JDK5. Now we have JDK9. Zheng-Liang Lu Java Programming 176 / 216

Example 1... 2 int[] A = {1, 2, 3}; 3 int sum = 0; 4 for (int i = 0; i < A.length; ++i) 5 sum += A[i]; 6... Not only is the syntax streamlined, but it also prevents boundary errors. 1... 2 int[] A = {1, 2, 3}; 3 int sum = 0; 4 for (int item: A) 5 sum += item; 6... Zheng-Liang Lu Java Programming 177 / 216

Short Introduction to Data Structures A data structure is a particular way of organizing data in a program so that it can be used efficiently. Data structures can implement one or more particular abstract data types (ADT), which specify the operations that can be performed on a data structure and the computational complexity of those operations. In comparison, a data structure is a concrete implementation of the specification provided by some ADT. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. 2 2 See http://bigocheatsheet.com/. Zheng-Liang Lu Java Programming 178 / 216

Common Operations on Data A specific data structure is chosen in one problem. Then the operations are implemented accordingly. The Arrays class contains useful methods for common array operations such as sorting and searching. For example, 1 import java.util.arrays; 2 3... 4 int[] A = {5, 2, 8}; 5 Arrays.sort(A); // sort the whole array 6 7 char[] B = { A, r, t, h, u, r }; 8 Arrays.sort(B, 1, 4); // sort the array partially 9... Zheng-Liang Lu Java Programming 179 / 216

Selection Sort 1... 2 // selection sort: O(n ˆ 2) time 3 for (int i = 0; i < A.length; i++) { 4 int k = i; // the position of min starting from i 5 for (int j = i + 1; j < A.length; j++) { 6 if (A[k] > A[j]) 7 k = j; 8 } 9 // swap(a[i], A[k]) 10 int tmp = A[k]; 11 A[k] = A[i]; 12 A[i] = tmp; 13 } 14... Time complexity: O(n 2 ) You can find more sorting algorithms. 3 3 See http://visualgo.net/. Zheng-Liang Lu Java Programming 180 / 216

Linear Search Write a program which searches for the index associated with the key. For convenience, assume that there is no duplicate key. The linear search approach compares the key with each element in the array sequentially. Zheng-Liang Lu Java Programming 181 / 216

1... 2 // assume A is an array 3 // linear search 4 for (int i = 0; i < A.length; i++) { 5 if (A[i] == key) { 6 System.out.printf("%3d", i); 7 } 8 } 9... Time complexity: O(n) Zheng-Liang Lu Java Programming 182 / 216

Alternative: Binary Search Time complexity: O(log n) Overall time complexity (sorting + searching): still O(log n)? Zheng-Liang Lu Java Programming 183 / 216

1... 2 int index = 1; // why? 3 int high = A.length 1, low = 0, mid; 4 while (high > low) { 5 mid = (high + low) / 2; 6 if (A[mid] == key) { 7 index = mid; 8 break; 9 } else if (A[mid] > key) 10 high = mid 1; 11 else 12 low = mid + 1; 13 } 14 15 if (index > 1) 16 System.out.printf("%d: %d\n", key, index); 17 else 18 System.out.printf("%d: does not exist\n", key); 19... Zheng-Liang Lu Java Programming 184 / 216

Beyond 1-Dimensional Arrays 2D or high-dimensional arrays are widely used. For example, a colorful image is represented by three 2D arrays (R, G, B). We can create a 2D T-type array with 4 rows and 3 columns as follows: 1... 2 int rowsize = 4; // row size 3 int colsize = 3; // column size 4 T[][] M = new T[rowSize][colSize]; 5... Zheng-Liang Lu Java Programming 185 / 216

Case (c) shows that we can create a 2D array by enumeration. Zheng-Liang Lu Java Programming 186 / 216

Reality Zheng-Liang Lu Java Programming 187 / 216

Example 4 1... 2 int[][] A = {{1, 2, 3}, {4, 5}, {6}}; 3 4 // conventional for loop 5 for (int i = 0; i < A.length; i++) { 6 for (int j = 0; j < A[i].length; j++) 7 System.out.printf("%2d", A[i][j]); 8 System.out.println(); 9 } 10 11 // for each loop 12 for (int[] row: A) { 13 for (int item: row) 14 System.out.printf("%2d", item); 15 System.out.println(); 16 } 17... 4 Thanks to a lively discussion on January 31, 2016. Zheng-Liang Lu Java Programming 188 / 216

Exercise: Matrix Multiplication Write a program which determines C = A B for the input matrices A m n and B n q for m, n, q N. You may use the formula c ij = n a ik b kj k=1 where a ik, i = 1, 2,..., m is a shorthand for A and b kj, j = 1, 2,..., q for B. Time complexity: O(n 3 ) (Why?) Zheng-Liang Lu Java Programming 189 / 216

1 class Lecture6 { 2 3 "Methods" 4 5 } 6 7 // keywords: 8 return Zheng-Liang Lu Java Programming 190 / 216

Methods 6 Methods can be used to define reusable code, and organize and simplify code. The idea of function originates from math, that is, y = f (x), where x is the input parameter 5 and y is the function value. In computer science, each input parameter should be declared with a specific type, and a function should be assigned with a return type. 5 Recall the multivariate functions. The input can be a vector, say the position vector (x, y, z). 6 Aka procedures and functions. Zheng-Liang Lu Java Programming 191 / 216

Example: max Zheng-Liang Lu Java Programming 192 / 216

1... 2 modifiers returntype methodname(listofparameters) { 3 // method body 4 } 5... modifiers could be static and public (for now). returntype could be primitive types and reference types. If the method does not return any value, then the return type is declared void. listofparameters is used to indicate the method inputs, each separated by commas. Note that a method could have no input. 7 The method name and the parameter list together are called the method signature. 8 7 For example, Math.random(). 8 Method overloading depends signatures. We will see it soon. Zheng-Liang Lu Java Programming 193 / 216

More Observations There are alternatives to the method max(): 1... 2 public static int max(int num1, int num2) { 3 if (num1 > num2) { 4 return num1; 5 } else { 6 return num2; 7 } 8 } 9... 1... 2 public static int max(int num1, int num2) { 3 return num1 > num2? num1 : num2; 4 } 5... Zheng-Liang Lu Java Programming 194 / 216

All roads lead to Rome. Anonymous 但如你根本並無招式, 敵人如何來破你的招式? 風清揚, 笑傲江湖 第十回 傳劍 Zheng-Liang Lu Java Programming 195 / 216

The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return statement, say main()); reaches a return statement; throws an exception (introduced later). As you can see, the return statement is not necessarily at the bottom of the method. 9 Once one defines the return type (except void), the method should guarantee to return a value or an object of that type. 9 Thanks to a lively discussion on November 22, 2015. Zheng-Liang Lu Java Programming 196 / 216

Bad Exampls 1... 2 public static int foo1() { 3 while (true); 4 return 0; // unreachable code 5 } 6 7 public static int foo2(int x) { 8 if (x > 0) { 9 return x; 10 } 11 // what if x < 0? 12 } 13... Zheng-Liang Lu Java Programming 197 / 216

Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, it s the obligation of callers to provide arguments in order, number, and compatible type, as defined in the method signature. Zheng-Liang Lu Java Programming 198 / 216

In Java, method invocation uses pass-by-value. When the callee is invoked, the program control (pc) is transferred from the caller to the callee. For each method invocation, JVM pushes a frame which stores necessary information in the call stack. The caller resumes its work once the callee finishes its routine. Zheng-Liang Lu Java Programming 199 / 216

Zheng-Liang Lu Java Programming 200 / 216

Variable Scope A variable scope refers to the region where a variable can be referenced. A pair of balanced curly braces defines the variable scope. In general, variables can be declared in class level, method level, or loop level. We cannot duplicate the variables whose names are identical in the same level. Zheng-Liang Lu Java Programming 201 / 216

1 public class ScopeDemo { 2 Example 3 public static int x = 10; // class level 4 5 public static void main(string[] args) { 6 7 System.out.println(x); // output 10 8 9 int x = 100; // method level, aka local variable 10 x++; 11 System.out.println(x); // output 101 12 addone(); 13 System.out.println(x); // output? 14 } 15 16 public static void addone() { 17 x = x + 1; 18 System.out.println(x); // output? 19 } 20 } Zheng-Liang Lu Java Programming 202 / 216

A Math Toolbox: Math Class The Math class provides basic mathematical functions and 2 global constants Math.PI 10 and Math.E 11. All methods are public and static. For example, max, min, round, ceil, floor, abs, pow, exp, sqrt, cbrt, log, log10, sin, cos, asin, acos, and random. Full document for Math class can be found here. You are expected to read the document! 10 The constant π is a mathematical constant, the ratio of a circle s circumference to its diameter, commonly approximated as 3.141593. 11 The constant e is the base of the natural logarithm. It is approximately equal to 2.71828. Zheng-Liang Lu Java Programming 203 / 216

Method Overloading Methods with the same name can coexist and be identified by the method signatures. 1... 2 public static int max(int x, int y) {... } 3 // different numbers of inputs 4 public static int max(int x, int y, int z) {... } 5 // different types 6 public static double max(double x, double y) {... } 7... Zheng-Liang Lu Java Programming 204 / 216

Recursion 12 Recursion is the process of defining something in terms of itself. A method that calls itself is said to be recursive. Recursion is an alternative form of program control. It is repetition without any loop. 12 Recursion is a commom pattern in nature. Zheng-Liang Lu Java Programming 205 / 216

Try Fractal. Zheng-Liang Lu Java Programming 206 / 216

Example The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than and equal to n. Note that 0! = 1. For example, 4! = 4 3 2 1 = 4 3! = 24. Can you find the pattern? n! = n (n 1)! In general, f (n) = n f (n 1). Zheng-Liang Lu Java Programming 207 / 216

Write a program which determines n!. 1... 2 public static int factorial(int n) { 3 if (n < 2) 4 return 1; // base case 5 else 6 return n factorial(n 1); 7 } 8... Note that there must be a base case in recursion. Time complexity: O(n) Can you implement the same method by using a loop? Zheng-Liang Lu Java Programming 208 / 216

Zheng-Liang Lu Java Programming 209 / 216

Equivalence: Loop Version 1... 2 int s = 1; 3 for (int i = 2; i <= n; i++) { 4 s = i; 5 } 6... Time complexity: O(n) One intriguing question is, Can we always turn a recursive method into a loop version of that? Yes, theoretically. 13 13 The Church-Turing Thesis proves it if the memory serves. Zheng-Liang Lu Java Programming 210 / 216

Remarks Recursion bears substantial overhead. So the recursive algorithm may execute a bit more slowly than the iterative equivalent. Additionally, a deeply recursive method depletes the call stack, which is limited, and causes stack overflow soon. Zheng-Liang Lu Java Programming 211 / 216

Memory Layout Zheng-Liang Lu Java Programming 212 / 216

Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers can be defined by the recurrence relation F n = F n 1 + F n 2, where n 2 and F 0 = 0, F 1 = 1. Zheng-Liang Lu Java Programming 213 / 216

1... 2 public static int fib(int n) { 3 if (n < 2) 4 return n; 5 else 6 return fib(n 1) + fib(n 2); 7 } 8... Straightforward and clear!!! Yet, this algorithm isn t efficient since it requires more time and memory. Time complexity: O(2 n ) (Why?!) Zheng-Liang Lu Java Programming 214 / 216

Zheng-Liang Lu Java Programming 215 / 216

1... 2 public static double fibiter(int n) { 3 if (n < 2) 4 return n; 5 6 int x = 0, y = 1; 7 for (int i = 2; i <= n; i++) { 8 int z = x + y; 9 x = y; 10 y = z; 11 } 12 return y; // why not z? 13 } 14... So it can be done in O(n) time. It implies that the previous one is not optimal. Could you find a linear recursion for Fibonacci numbers? In fact, this problem can be done in O(log n) time! Zheng-Liang Lu Java Programming 216 / 216