Public-Service Announcement

Similar documents
Public-Service Announcement

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

Public-Service Announcement

CS61B Lecture #2. Public Service Announcements:

Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

Public-Service Announcements

Public-Service Announcements

CS61B Lecture #6: Arrays

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Last time s big ideas

Public-Service Announcement

CS 1110: Introduction to Computing Using Python Loop Invariants

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS61B Lecture #11: Examples: Comparable & Reader

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

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

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

Another interface: Comparable

CSE 2123: Collections: Priority Queues. Jeremy Morris

Arrays and while Loops. Lecture 15 - COMP110 - Spring 2018

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

"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

Week 6 CS 302. Jim Williams, PhD

"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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CSIS 10B Lab 2 Bags and Stacks

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CS61B Lecture #7. Announcements:

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

AP Computer Science A Summer Assignment 2017

last time in cs recitations. computer commands. today s topics.

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

Recreation. Last modified: Mon Sep 21 12:46: CS61B: Lecture #11 1

CS111: PROGRAMMING LANGUAGE II

CMPSCI 187 / Spring 2015 Sorting Kata

Lecture 2. CS118 Term planner. Refinement. Recall our first Java program. Program skeleton GCD. For your first seminar. For your second seminar

CS61B Lecture #5: Arrays and Objects

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Recursion. CSCI 112: Programming in C

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

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

"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

Software and Programming 1

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

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

Intro to Algorithms. Professor Kevin Gold

int b = 2; for (int a = 0; a < 10 b < 20; a++) { a = a + b; b = b + a; }

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

RECURSION. Week 6 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on material by James Barker. Pre-Laboratory Checklist

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

STRINGS AND STRINGBUILDERS. Spring 2019

CITS1001 week 4 Grouping objects

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

protected BinaryNode root; } 02/17/04 Lecture 11 1

COS 126 General Computer Science Spring Written Exam 1

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

CS13002 Programming and Data Structures, Spring 2005

ECSE 321 Assignment 2

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

CS3: Introduction to Symbolic Programming. Lecture 5:

Announcements. 1. Forms to return today after class:

Overview. Lab 5 Methods and Parameters

Lecture 11: Recursion (hard)

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Final Examination Semester 2 / Year 2011

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Dm507 project 1. Emil Sjulstok Rasmussen Hold: M1

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Unit 10: Sorting/Searching/Recursion

ISY00245 Principles of Programming. Module 7

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Testing and Debugging

CSE143 Notes for Monday, 4/25/11

CS61B, Fall 2014 Project #1 (revision 4) P. N. Hilfinger

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Correctness of specifications. Correctness. Correctness of specifications (2) Example of a Correctness Proof. Testing versus Correctness Proofs

CS61B Lecture #11. Please report problems (missing files, malfunctions of submit, etc.) by , not by the newsgroup, for faster service.

Today: Sorting algorithms: why? Insertion Sort. Inversions. CS61B Lecture #26. Last modified: Wed Oct 24 13:43: CS61B: Lecture #26 1

Advanced Algorithms and Data Structures

1. An operation in which an overall value is computed incrementally, often using a loop.

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

New Concepts. Lab 7 Using Arrays, an Introduction

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Computer Programming: C++

Bjarne Stroustrup. creator of C++

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

CSE 131 Introduction to Computer Science Fall Exam II

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

1st Semester Examinations CITS1001 3

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Transcription:

Public-Service Announcement CodeBase is a student organization that does technical consulting for startups. It is an opportunity for students to design, develop, and consult on real world technical challenges that companies face as well as build an impressive portfolio and resume. We are looking for talented developers and designers to work with us on industry projects. If youâ re interested come learn more at our infosession! [Thursday at 7PM in Anna Head Alumnae Hall] See http://info.codebase.club. Learn more about us at: http://www.codebase.club Apply at: http://www.codebase.club/students/ Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 1

CS61B Lecture #3 Labs: We are forgiving during the first week or so, but try to get your lab1 submitted properly. DBC: Let us know if you can t get something to work! Thereare50 100peoplewhodonotyethaveGitrepositories(which you need to complete the lab). Do you this done! If you are on the waiting list, you will not be admitted until you find an open lab (or a space opens up in the one you are waiting on). Remove yourself from the one you are waiting on and enroll for an open one, or risk not getting in. Tests weeks of Sept 28 and Oct 26. We ll be more specific when we get rooms. Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 2

More Iteration: Sort an Array Problem. Print out the command-line arguments in order: % java sort the quick brown fox jumped over the lazy dog brown dog fox jumped lazy over quick the the Plan. public class Sort { /** Sort and print WORDS lexicographically. */ public static void main(string[] words) { sort(words, 0, words.length-1); print(words); z /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { /* "TOMORROW" */ /** Print A on one line, separated by blanks. */ static void print(string[] A) { /* "TOMORROW" */ Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 3

How do We Know If It Works? Unit testing refers to the testing of individual units(methods, classes) within a program, rather than the whole program. In this class, we mainly use the JUnit tool for unit testing. Example: AGTestYear.java in lab #1. Integration testing refers to the testing of entire (integrated) set of modules the whole program. In this course, we ll look at various ways to run the program against prepared inputs and checking the output. Regression testing refers to testing with the specific goal of checking that fixes, enhancements, or other changes have not introduced faults (regressions). Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 4

Idea: write tests first. Test-Driven Development Implement unit at a time, run tests, fix and refactor until it works. We re not really going to push it in this course, but it is useful and has quite a following. Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 5

Testing sort This is pretty easy: just give a bunch of arrays to sort and then make sure they each get sorted properly. Have to make sure we cover the necessary cases: Corner cases. E.g., empty array, one-element, all elements the same. Representative middle cases. E.g., elements reversed, elements in order, one pair of elements reversed,... Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 6

Simple JUnit The JUnit package provides some handy tools for unit testing. The Java annotation @Test on a method tells the JUnit machinery to call that method. (An annotation in Java provides information about a method, class, etc., that can be examined within Java itself.) A collection of methods with names beginning with assert then allow your test cases to check conditions and report failures. [See example.] Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 7

Selection Sort /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { if (L < U) { int k = /*( Index s.t. A[k] is largest in A[L],...,A[U] )*/; /*{ swap A[k] with A[U] */; /*{ Sort items L to U-1 of A. */; And we re done! Well, OK, not quite. Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 8

Selection Sort /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { if (L < U) { int k = indexoflargest(a, L, U); /*{ swap A[k] with A[U] */; /*{ Sort items L to U-1 of A. */; Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 9

Selection Sort /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { if (L < U) { int k = indexoflargest(a, L, U); /*{ swap A[k] with A[U] */; sort(a, L, U-1); // Sort items L to U-1 of A Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 10

Selection Sort /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { if (L < U) { int k = indexoflargest(a, L, U); String tmp = A[k]; A[k] = A[U]; A[U] = tmp; sort(a, L, U-1); // Sort items L to U-1 of A Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 11

Selection Sort /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { if (L < U) { int k = indexoflargest(a, L, U); String tmp = A[k]; A[k] = A[U]; A[U] = tmp; sort(a, L, U-1); // Sort items L to U-1 of A What would an iterative version look like? while (?) {? Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 12

Selection Sort /** Sort items A[L..U], with all others unchanged. */ static void sort(string[] A, int L, int U) { if (L < U) { int k = indexoflargest(a, L, U); String tmp = A[k]; A[k] = A[U]; A[U] = tmp; sort(a, L, U-1); // Sort items L to U-1 of A Iterative version: while (L < U) { int k = indexoflargest(a, L, U); String tmp = A[k]; A[k] = A[U]; A[U] = tmp; U -= 1; Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 13

Find Largest /** Index k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (?) return i1; else { Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 14

Find Largest /** Index k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 15

Find Largest /** Index k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = /*( index of largest value in V[i0 + 1..i1] )*/; return /*( whichever of i0 and k has larger value )*/ Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 16

Find Largest /** Index k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = indexoflargest(v, i0 + 1, i1); return /*( whichever of i0 and k has larger value )*/ Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 17

Find Largest /** Index k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = indexoflargest(v, i0 + 1, i1); return (V[i0].compareTo(V[k]) > 0)? i0 : k; // if (V[i0].compareTo(V[k]) > 0) return i0; else return k; Turning this into an iterative version is tricky: not tail recursive. What are the arguments to compareto the first time it s called? Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 18

Iteratively Find Largest /** Value k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = indexoflargest(v, i0 + 1, i1); return (V[i0].compareTo(V[k]) > 0)? i0 : k; // if (V[i0].compareTo(V[k]) > 0) return i0; else return k; Iterative: int i, k; k =?; // Deepest iteration for (i =?;...?; i...?) k =?; return k; Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 19

Iteratively Find Largest /** Value k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = indexoflargest(v, i0 + 1, i1); return (V[i0].compareTo(V[k]) > 0)? i0 : k; // if (V[i0].compareTo(V[k]) > 0) return i0; else return k; Iterative: int i, k; k = i1; // Deepest iteration for (i =?;...?; i...?) k =?; return k; Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 20

Iteratively Find Largest /** Value k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = indexoflargest(v, i0 + 1, i1); return (V[i0].compareTo(V[k]) > 0)? i0 : k; // if (V[i0].compareTo(V[k]) > 0) return i0; else return k; Iterative: int i, k; k = i1; // Deepest iteration for (i = i1-1; i >= i0; i -= 1) k =?; return k; Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 21

Iteratively Find Largest /** Value k, I0<=k<=I1, such that V[k] is largest element among * V[I0],... V[I1]. Requires I0<=I1. */ static int indexoflargest(string[] V, int i0, int i1) { if (i0 >= i1) return i1; else /* if (i0 < i1) */ { int k = indexoflargest(v, i0 + 1, i1); return (V[i0].compareTo(V[k]) > 0)? i0 : k; // if (V[i0].compareTo(V[k]) > 0) return i0; else return k; Iterative: int i, k; k = i1; // Deepest iteration for (i = i1-1; i >= i0; i -= 1) k = (V[i].compareTo(V[k]) > 0)? i : k; return k; Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 22

Finally, Printing /** Print A on one line, separated by blanks. */ static void print(string[] A) { for (int i = 0; i < A.length; i += 1) System.out.print(A[i] + " "); System.out.println(); /* J2SE 5 introduced a new syntax for the \{\bf for\ * loop here: */ for (String s : A) System.out.print(s + " "); /* Use it if you like, but let s not stress over it yet! */ Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 23

Another Problem Given an array of integers, A, move its last element, A[A.length-1], to just after nearest previous item that is to it (shoving other elements to the right). For example, if A starts out as { 1, 9, 4, 3, 0, 12, 11, 9, 15, 22, 12 then it ends up as { 1, 9, 4, 3, 0, 12, 11, 9, 12, 15, 22 If there is no such previous item, move A[A.length-1] to the beginning of A (i.e., to A[0]). So { 1, 9, 4, 3, 0, 12, 11, 9, 15, 22, -2 would become { -2, 1, 9, 4, 3, 0, 12, 11, 9, 15, 22 (Preliminary question: How can I state this without making this last case special?) Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 24

Your turn public class Shove { /** Move A[A.length-1] so that it is just after the nearest * previous item that is <= A[A.length-1], or to A[0] if * there isn t such an item. Move all succeeding items * to the right (i.e., up one index). */ // BETTER DESCRIPTION? static void moveover(int[] A) { // FILL IN Last modified: Mon Aug 29 14:41:43 2016 CS61B: Lecture #3 25