Public-Service Announcement I

Similar documents
Public-Service Announcement

CS61B Lecture #6: Arrays

CS61B Lecture #5: Arrays and Objects

CS61B Lecture #2. Public Service Announcements:

Public-Service Announcement

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Backward Reasoning: Rule for Assignment. Backward Reasoning: Rule for Sequence. Simple Example. Hoare Logic, continued Reasoning About Loops

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

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

CS18000: Programming I

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

Chapter 9 Introduction to Arrays. Fundamentals of Java

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

You must bring your ID to the exam.

Laboratorio di Algoritmi e Strutture Dati

Public-Service Announcements

Loops. When to Use Loops

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Go Bears! IE170: Algorithms in Systems Engineering: Lecture 4

Lecture Compiler Middle-End

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Memory and C++ Pointers

CSE 2123 Sorting. Jeremy Morris

This page intentionally left blank

Programming for Electrical and Computer Engineers. Pointers and Arrays

Topic 22 arrays - part 2. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

University of Palestine. Mid Exam Total Grade: 100

Lecture 2: Getting Started

Data Structures Brett Bernstein

Public-Service Announcement

Public-Service Announcements

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

Lecture 23: Parallelism. Parallelism Idea. Parallel Programming in Java. CS 62 Fall 2015 Kim Bruce & Michael Bannister

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

COMPUTER SCIENCE. 4. Arrays. Section 1.4.

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

Public-Service Announcement

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

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

Scope of this lecture

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

All answers will be posted on web site, and most will be reviewed in class.

BİLGE KÖROĞLU. Lecture Notes (May 2 4, 2007) METHOD DECOMPOSITION and ARRAYS

CS111: PROGRAMMING LANGUAGE II

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

3/19/2015. Chapter 19 Sorting and Searching SELECTION SORT PROFILING THE SELECTION SORT ALGORITHM SELECTION SORT ON VARIOUS SIZE ARRAYS*

Arrays and ArrayLists. Ananda Gunawardena

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Computer Science II Data Structures

Computer Science & Engineering 150A Problem Solving Using Computers

Welcome to... CS113: Introduction to C

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key

CSCI-GA Scripting Languages

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Computer Programming

FORM 2 (Please put your name and form # on the scantron!!!!)

Computer Science 331

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

Design and Analysis of Algorithms Assignment-2 Solutions (Hints) REMARK: Quicksort

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CS 310: Order Notation (aka Big-O and friends)

CP122 CS I. Iteration

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Solutions Manual. Data Structures and Algorithms in Java, 5th edition International Student Version. M. T. Goodrich and R.

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

NOTE: Answer ANY FOUR of the following 6 sections:

CSE 373: Data Structures and Algorithms

CS 211: Methods, Memory, Equality

Arrays. Eng. Mohammed Abdualal

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

CSE 332: Data Structures & Parallelism Lecture 5: Algorithm Analysis II. Ruth Anderson Winter 2018

DO NOT. In the following, long chains of states with a single child are condensed into an edge showing all the letters along the way.

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

1.00/ Lecture 8. Arrays-1

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Problems with simple variables

Lecture 29: Parallelism II

CS 251 Intermediate Programming Java Basics

Public-Service Announcement

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Cost Models. Chapter Twenty-One Modern Programming Languages, 2nd ed. 1

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!

Transcription:

Public-Service Announcement I Are you an engineer, designer, or entrepreneur? Come check out Mobile Developers of Berkeley. We create end-to-end solutions with a diverse breath of skills. Our members hone their skills in frontend, backend, design, and engineering while developing an understanding of topics such as software architecture, databases, operating systems, and networking. We build apps targeting a wide range of industries while leveraging the latest technologies in IOT, AI, fintech, ML, AR, and more. Join us for our showcase tonight, Wednesday 9/6. Our members will show you our apps and what it took to make them. Free food, fidget spinners & swag will also be there. See https://www.mobiledevsberkeley.org/ for further information. Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 1

Public-Service Announcement II Introducing Political Computer Science @ Berkeley! PCS harnesses students intellectual capabilities and potential in CS and Government affairs to address current issues within the United States legal and political systems. We already have an Amazon Alexa Skill called Political Pundit that is about to be published; it is able to give responses to inquiries about politics/government. To view the many other projects that we have in the works (pertaining to gerrymandering, campaign finance, Alexa, and more), visit our website: pcsberkeley.wixsite.com/pcsberkeley We re now accepting applications for the 2017-2018 school year (it only takes 5 minutes to complete)! Apply on our website! Recreation What is the sum of the coefficients of (1 3x+3x 2 ) 743 (1+3x 3x 2 ) 744 after expanding and collecting terms? Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 2

CS61B Lecture #6: Arrays An array is a structured container whose components are length, a fixed integer. a sequence of length simple containers of the same type, numbered from 0. (.length field usually implicit in diagrams.) Arrays are anonymous, like other structured containers. Always referred to with pointers. For array pointed to by A, Length is A.length Numbered component i is A[i] (i is the index) Important feature: index can be any integer expression. Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 3

A Few Samples Java Results int[] x, y, z; String[] a; x = new int[3]; y = x; a = new String[3]; x[1] = 2; y[1] = 3; a[1] = "Hello"; int[] q; q = new int[] { 1, 2, 3 ; // Short form for declarations: int[] r = { 7, 8, 9 ; x: 0 3 0 y: z: a: Hello q: 1 2 3 r: 7 8 9 Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 4

Example: Accumulate Values Problem: Sum up the elements of array A. static int sum(int[] A) { int N; N = 0; // New (1.5) syntax for (int i = 0; i < A.length; i += 1) for (int x : A) N += A[i]; N += x; return N; // For the hard-core: could have written int N, i; for (i=0, N=0; i<a.length; N += A[i], i += 1) { // or just ; // But please don t: it s obscure. Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 5

Example: Insert into an Array Problem: Want a call like insert(a, 2, "gnu") to convert (destructively) A: bear gazelle hartebeest skunk to A: bear gazelle gnu hartebeest /** Insert X at location K in ARR, moving items K, K+1,... to locations * K+1, K+2,... The last item in ARR is lost. */ static void insert (String[] arr, int k, String x) { for (int i = arr.length-1; i > k; i -= 1) // Why backwards? arr[i] = arr[i-1]; /* Alternative to this loop: System.arraycopy(arr, k, {{ from arr[k] = x; arr, k+1, {{ to arr.length-k-1);*/ {{ # to copy Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 6

(Aside) Java Shortcut Useful tip: Can write just arraycopy by including at the top of the source file: import static java.lang.system.arraycopy; This means define the simple name arraycopy to be the equivalent of java.lang.system.arraycopy in the current source file. Can do the same for out so that you can write out.println(...); in place of System.out.println(...); Finally, a declaration like import static java.lang.math.*; means take all the (public) static definitions in java.lang.math and make them available in this source file by their simple names (the name after the last dot). Useful for functions like sin, sqrt, etc. Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 7

Growing an Array Problem: Suppose that we want to change the description above, so that A = insert2 (A, 2, "gnu") does not shove skunk off the end, but instead grows the array. A: bear gazelle hartebeest skunk to A: bear gazelle gnu hartebeest skunk /** Return array, r, where r.length = ARR.length+1; r[0..k-1] * the same as ARR[0..K-1], r[k] = x, r[k+1..] same as ARR[K..]. */ static String[] insert2(string[] arr, int k, String x) { String[] result = new String[arr.length + 1]; arraycopy(arr, 0, result, 0, k); arraycopy(arr, k, result, k+1, arr.length-k); result[k] = x; return result; Why do we need a different return type from insert2?? Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 8

Example: Merging Problem: Given two sorted arrays of ints, A and B, produce their merge: a sorted array containing all items from A and B. A: 0 2 3 6 9 11 B: 1 4 5 7 8 result: 0 1 2 3 4 5 6 7 8 9 11 Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 9

Example: Merging Program Problem: Given two sorted arrays of ints, A and B, produce their merge: a sorted array containing all from A and B. Remark: In order to solve this recursively, it is useful to generalize the original function to allow merging portions of the arrays. /** Assuming A and B are sorted, returns their merge. */ public static int[] merge(int[] A, int[] B) { return merge(a, 0, B, 0); /** The merge of A[L0..] and B[L1..] assuming A and B sorted. */ static int[] merge(int[] A, int L0, int[] B, int L1) { int N = A.length - L0 + B.length - L1; int[] C = new int[n]; if (L0 >= A.length) arraycopy(b, L1, C, 0, N); else if (L1 >= B.length) arraycopy(a, L0, C, 0, N); else if (A[L0] <= B[L1]) { C[0] = A[L0]; arraycopy(merge(a, L0+1, B, L1), 0, C, 1, N-1); else { C[0] = B[L1]; arraycopy(merge(a, L0, B, L1+1), 0, C, 1, N-1); return C; What is wrong with this implementation? Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 10

A Tail-Recursive Strategy public static int[] merge(int[] A, int[] B) { return merge(a, 0, B, 0, new int[a.length+b.length], 0); /** Merge A[L0..] and B[L1..] into C[K..], assuming A and B sorted. */ static int[] merge(int[] A, int L0, int[] B, int L1, int[] C, int k){... This last method merges part of A with part of B into part of C. For example, consider a possible call merge(a, 3, B, 1, C, 2) L0 L1 A: -1 0 2 3 6 9 B: 1 4 5 7 8 C:?? 3 4 5 6 7 8 9?? k Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 11

A Tail-Recursive Solution public static int[] merge(int[] A, int[] B) { return merge(a, 0, B, 0, new int[a.length+b.length], 0); /** Merge A[L0..] and B[L1..] into C[K..], assuming A and B sorted. */ static int[] merge(int[] A, int L0, int[] B, int L1, int[] C, int k){ if (L0 >= A.length) /*? */ else if (L1 >= B.length) /*? */ else if (A[L0] <= B[L1]) { C[k] = A[L0]; /*? */ else { C[k] = B[L1]; /*? */ return C; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 12

A Tail-Recursive Solution public static int[] merge(int[] A, int[] B) { return merge(a, 0, B, 0, new int[a.length+b.length], 0); /** Merge A[L0..] and B[L1..] into C[K..], assuming A and B sorted. */ static int[] merge(int[] A, int L0, int[] B, int L1, int[] C, int k){ if (L0 >= A.length) /*? */ else if (L1 >= B.length) /*? */ else if (A[L0] <= B[L1]) { C[k] = A[L0]; /*? */ else { C[k] = B[L1]; /*? */ return C; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 13

A Tail-Recursive Solution public static int[] merge(int[] A, int[] B) { return merge(a, 0, B, 0, new int[a.length+b.length], 0); /** Merge A[L0..] and B[L1..] into C[K..], assuming A and B sorted. */ static int[] merge(int[] A, int L0, int[] B, int L1, int[] C, int k){ if (L0 >= A.length) arraycopy(b, L1, C, k, B.length-L1); else if (L1 >= B.length) arraycopy(a, L0, C, k, A.length-L0); else if (A[L0] <= B[L1]) { C[k] = A[L0]; /*? */ else { C[k] = B[L1]; /*? */ return C; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 14

A Tail-Recursive Solution public static int[] merge(int[] A, int[] B) { return merge(a, 0, B, 0, new int[a.length+b.length], 0); /** Merge A[L0..] and B[L1..] into C[K..], assuming A and B sorted. */ static int[] merge(int[] A, int L0, int[] B, int L1, int[] C, int k){ if (L0 >= A.length) arraycopy(b, L1, C, k, B.length-L1); else if (L1 >= B.length) arraycopy(a, L0, C, k, A.length-L0); else if (A[L0] <= B[L1]) { C[k] = A[L0]; merge(a, L0+1, B, L1, C, k+1); else { C[k] = B[L1]; merge(a, L0, B, L1+1, C, k+1); return C; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 15

Iterative Solution In general, we don t use either of the previous approaches in languages like C and Java. Array manipulation is most often iterative: public static int[] merge(int[] A, int[] B) { int[] C = new int[a.length + B.length]; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 16

Iterative Solution II public static int[] merge(int[] A, int[] B) { int[] C = new int[a.length + B.length]; int L0, L1; L0 = L1 = 0; for (int k = 0; k < C.length; k += 1) { if (L0 >= A.length) { C[k] = B[L1]; L1 += 1; else if (L1 >= B.length) { C[k] = A[L0]; L0 += 1; else if (A[L0] <= B[L1]) { C[k] = A[L0]; L0 += 1; else { C[k] = B[L1]; L1 += 1; return C; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 17

Alternative Solution: Removing k Claim: An invariant of the loop is that k=l0+l1. public static int[] merge(int[] A, int[] B) { int[] C = new int[a.length + B.length]; int L0, L1; L0 = L1 = 0; while (L0 + L1 < C.length) { if (L0 >= A.length) { C[L0 + L1] = B[L1]; L1 += 1; else if (L1 >= B.length) { C[L0 + L1] = A[L0]; L0 += 1; else if (A[L0] <= B[L1]) { C[L0 + L1] = A[L0]; L0 += 1; else { C[L0 + L1] = B[L1]; L1 += 1; return C; Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 18

Multidimensional Arrays What about two- or higher-dimensional layouts, such as A = 2 3 4 5 4 9 16 25 8 27 64 125? Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 19

Multidimensional Arrays in Java These are not primitive in Java, but we can build them as arrays of arrays: int[][] A = new int[3][]; A[0] = new int[] {2, 3, 4, 5; A[1] = new int[] {4, 9, 16, 25; A[2] = new int[] {8, 27, 64, 125; // or int[][] A; A = new int[][] { {2, 3, 4, 5, {4, 9, 16, 25, A: { 8, 27, 64, 125 ; // or int[][] A = { {2, 3, 4, 5, {4, 9, 16, 25, {8, 27, 64, 125 ; // or int[][] A = new A[3][4]; for (int i = 0; i < 3; i += 1) for (int j = 0; j < 4; j += 1) A[i][j] = (int) Math.pow(j + 2, i + 1); 2 3 4 5 4 9 16 25 8 27 64125 Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 20

Exotic Multidimensional Arrays Since every element of an array is independent, there is no single width in general: int[][] A = new int[5][]; A[0] = new int[] {; A[1] = new int[] {0, 1; A[2] = new int[] {2, 3, 4, 5; A[3] = new int[] {6, 7, 8; A[4] = new int[] {9; What does this print? int[][] ZERO = new int[3][]; ZERO[0] = ZERO[1] = ZERO[2] = new int[] {0, 0, 0; ZERO[0][1] = 1; System.out.println(ZERO[2][1]); A: 0 1 2 3 4 5 6 7 8 2 9 Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 21

Exotic Multidimensional Arrays Since every element of an array is independent, there is no single width in general: int[][] A = new int[5][]; A[0] = new int[] {; A[1] = new int[] {0, 1; A[2] = new int[] {2, 3, 4, 5; A[3] = new int[] {6, 7, 8; A[4] = new int[] {9; What does this print? int[][] ZERO = new int[3][]; ZERO[0] = ZERO[1] = ZERO[2] = new int[] {0, 0, 0; ZERO[0][1] = 1; System.out.println(ZERO[2][1]); A: A: 0 1 2 3 4 5 6 7 8 2 9 0 1 0 Last modified: Tue Sep 5 16:09:38 2017 CS61B: Lecture #6 22