CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5

Slides:



Advertisements
Similar presentations
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Advertisements

The Importance of Punctuation Dear Jack. A woman, without her man, is nothing. A woman, without her man, is nothing. A woman: without her, man is nothing.
Dear John, I want a man who knows what love is all about
THE WRITE STUFF! Do You Have It?. Why is it important to be able to write well/effectively?
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
CS100J Nov 04, 2003 Arrays --sorting. Reading: 8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous kind.
1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
1 Teaching the development of algorithms Teach skill, not only facts David Gries Computer Science Department Cornell University November 2004 DrJava is.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
1 CS100J 29 March 2007 Arrays: searching & sorting. Reading: 8.5 Why did I get a Christmas Card on Halloween? Searching and sorting algorithms are on the.
1 CS100J 31 October 2006 Arrays: searching & sorting. Reading: 8.5 Merry Christmas!! On Halloween? Searching and sorting algorithms are on the course website.
Final Prep workshop for your final project “Make your Own Lab”
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
PUNCTUATIONPUNCTUATIONPUNCTUATIONPUNCTUATION.?,:; ! “ ”
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
1 CS November 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
1 CS100J 18 October 2005 Arrays Reading: You are responsible for: Secs 8.1, 8.2, 8.3, 8.4 A decimal point I'm a dot in placeSlot machines Cash lost in.
Conventions Woman without her man has no reason for living. Woman: without her, man has no reason for living.
1 CS April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 29 March 2005 Arrays Reading: You are responsible for: Secs 8.1, 8.2, 8.3, 8.4 A decimal point I'm a dot in placeSlot machines Cash lost in 'em.
The Power of Punctuation Remember…. Eats, shoots and leaves? Think about that with this love letter.
Examples of Punctuation
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
Personal Statements PS Bath. Personal Statement – guess the right answer.
Recognising Opportunities to Include Functional Skills English Lucy Crofts.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
1 CS100J 21 March 2006 Arrays: searching & sorting. Reading: 8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are.
PUNCTUATION : Making your message clear. PUNCTUATION : MAKING YOUR MESSAGE CLEAR.
RHETORICAL GRAMMAR LESSON #22. OUTCOME 1 To demonstrate an awareness of the strategies that writers use in different writing contexts.
Remind students of last week’s T4TW
SWEN421 – Lecture 4 Contracts and Correctness
Correctness issues and Loop invariants
. ? , : ; ! “ ” P U N C T U A T I O N.
Why it’s important to get it right!
Developing Loops from Invariants
Announcements P2 is due tomorrow Prelim on Monday
Testing change to a linked list
A True Definition! Hopefully?!
"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 Lecture 11 CS2110 – Fall 2018.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Why Punctuation Matters
Asymptotic complexity Searching/sorting
"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 Lecture 11 CS2110 – Spring 2019.
Binary Search and Loop invariants
Asymptotic complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS November 2010 Developing array algorithms. Reading:
Sorting and Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Developing loops from invariants
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Linear and Binary Search
Commas.
CS100J Nov 04, 2003 Arrays. Reading: 8
The importance of syntax…
Author’s /IntentPurpose Meets Grammar
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5 The searching, sorting, and other algorithms will be on the course website, along with a JUnit testing class for them. Please punctuate this: Dear John, I want a man who knows what love is all about you are generous kind thoughtful people who are not like you admit to being useless and inferior you have ruined me for other men I yearn for you I have no feelings whatsoever when we're apart I can be forever happy will you let me be yours Gloria  

This is a neat example of the ambiguity that English can cause, if not used properly! We try to use English properly and precisely, but ambiguity tends to creep in because of difference in cultures in which people grow up and simply because of differences of opinion. Read on! Dear John: I want a man who knows what love is all about. You are generous, kind, thoughtful. People who are not like you admit to being useless and inferior. You have ruined me for other men. I yearn for you. I have no feelings whatsoever when we're apart. I can be forever happy -- will you let me be yours? Gloria   I want a man who knows what love is. All about you are generous, kind, thoughtful people, who are not like you. Admit to being useless and inferior. You have ruined me. For other men, I yearn. For you, I have no feelings whatsoever. When we're apart, I can be forever happy. Will you let me be? Yours, Gloria .

Today and Thursday • Look at horizontal notation for writing assertions about arrays. • Develop several methods that process arrays. The idea is to help you learn how to develop algorithms. • Write a function to tell whether two arrays are equal. • Write a function to copy an array. • Write a function to tell whether two DNA sequences are complements of each other. • Look at other algorithms that manipulate arrays • Look at storing a table of values in a Java array. including adding a value to the table, deleting the last value of the table, deleting some other value from the table. The material on tables is in Sec. 8.4 of course text.

Horizontal notation for arrays, strings, Vectors b <= sorted >= 0 k b.length Example of an assertion about an array b. It asserts that: b[0..k–1] is sorted (i.e. its values are in ascending order) Everything in b[0..k–1] is ≤ everything in b[k..b.length–1] b <= sorted >= 0 k b.length b[0..k] is sorted (i.e. its values are in ascending order) Everything in b[0..k] is ≤ everything in b[k+1..b.length–1]

Maintain a table of values in an array As a program is executed, it may have to maintain a table of values, say temperatures, within an array. The table will start out empty; then values will be added to it. We must say where in the array the values are stored. int[] b= new int[5000]; // The n values in the table are in b[0..n–1] int n= 0; // 0 ≤ n <= 5000 b table of values this part is unused 0 n b.length // Add t to the table: b[n]= t; n= n+1; // Delete last element of table // (assuming it exists). n= n –1; Deleting an element of the table doesn't require to change the array at all! It's just that a value that was in the table is now in the unused part.

Maintain a table of values in an array b table of values this part is unused 0 j n b.length // Delete value b[j] from the table. If the order of values in the table doesn’t matter: n= n–1; b[j]= b[n]; If the order of values in table does matter: n= n–1; // Move b[j+1..n] to b[j..n–1] // inv: b[j+1..k–1] have been moved for (int k= j+1; k <= n; k= k+1 ) { // Process b[k] (move it down 1 pos.) b[k–1]= b[k]; } // post: b[j+1..n] have been moved

i= 0; while (b[i] != x) { i= i + 1; } 0 n precondition: b Find first position of x in array b. x is guaranteed to be in the array. We use the horizontal notation to write assertions about arrays. ? (x is in here) 0 n precondition: b x not here x ? (x is in here) 0 i n postcondition: b The invariant is found easily from the postcondition x not here ? (x is in here) 0 i n invariant: b i= 0; while (b[i] != x) { i= i + 1; } 1. Makes invariant true 2. When this is true, so is postcondition 3. Make progress toward termination 4. Invariant is true after repetend.

• Dutch national flag. Vague spec. : b[0 • Dutch national flag.Vague spec.: b[0..n-1] contains only red, white, blue balls. Sort it using only swaps. Better spec.: Precondition: n >= 0 Permute b[0..n–1] to truthify: postcondition: b[0..h–1] are red balls b[h..k–1] are white balls b[k..n–1] are blue balls precondition: postcondition: invariant : 0 n ? 0 h k n reds whites blues

h k Partition algorithm: Permute the values of the array and store a value in j to truthify: pre: x ? ≤ x x ≥ x h j k post: x ≤ x ? ≥x h i j k inv:

• Linear search.Vague spec.: find first occurrence of v in b[h..k-1]. Better spec.: Store an integer in i to truthify: postcondition: (0) v is not in b[h..i-1] (1) Either i= k or v = b[k] invariant: v is not in b[h..i-1] • Finding the min. Vague spec.: Find the min of b[h..k] Better spec.: Precondition: h <= k (because an empty set of values has no min) Store in i to truthify: postcondition: b[m] is the min of b[h..k] (and it is first occurrence of the min) invariant: b[m] is the min of b[h..t-1] (and it is first occur. of the min) • Binary search: Vague spec: Look for v in sorted array segment b[h..k]. Better spec: Precondition: b[h..k] is sorted (in ascending order). postcondition: b[h..i] <= v and v < b[i+1..k] invariant: b[h..i] <= v and v < b[j..k]