CS November 2010 Developing array algorithms. Reading:

Slides:



Advertisements
Similar presentations
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Advertisements

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
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 14 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,
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
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.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
Two –Dimensional Arrays Mrs. C. Furman Java Programming November 19, 2008.
1 CS100J 31 October 2006 Arrays: searching & sorting. Reading: 8.5 Merry Christmas!! On Halloween? Searching and sorting algorithms are on the course website.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
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.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
1 CS November 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
1 CS Nov 2011 Ragged arrays Reading for today: sec Reading for next time: Interfaces: Sec and corresponding ProgramLive material. Also,
1 CS April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
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.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
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 CS Oct 2008 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, and.
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.
Chapter 9: Sorting and Searching Arrays
Chapter 6: Using Arrays.
Two-Dimensional Arrays
Correctness issues and Loop invariants
Recitation 13 Searching and Sorting.
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Week 13 - Monday CS 121.
Chapter 7 Arrays.
Developing Loops from Invariants
Selection Sort – an array sorting algorithm
Announcements P2 is due tomorrow Prelim on Monday
Testing change to a linked list
Lab 4: C Array Review: Array Lab assignments.
"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.
Sorting … and Insertion Sort.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Data Structures (CS212D) Week # 2: Arrays.
Given value and sorted array, find index.
Arrays Week 2.
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
Multi-Dimensional Arrays
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
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
CS100J Nov 04, 2003 Arrays. Reading: 8
CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9
Developing Loops from Invariants
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

CS1110 4 November 2010 Developing array algorithms. Reading: 8.3..8.5 Prelim Tuesday, 7:30PM. Olin 155 and 255 Review session, Sunday 1–3. Phillips 101 Handout describes what will be covered. have iclickers ready for quiz

Two-dimensional arrays 5 4 7 3 b 0 1 2 3 b.length one-dimensional array 5 4 7 3 4 8 9 7 5 1 2 3 0 1 2 3 0 1 2 rectangular array: 5 rows and 4 columns d Type of d is int[][] (“int array array”, “an array of int arrays”) To declare variable d: int d[][]. To create a new array and assign it to d: d= new int[3][4]; To reference element at row r column c: d[r][c] number of rows number of cols

A 2-dimensional array b P00 P01 P02 P03 P10 P11 P12 P13 P20 P21 P22 P23 Same array in row-major order: c P00 P01 P02 P03 P10 P11 P12 P13 P20 P21 P22 P23 You can see that b[0][j] is same as c[0 * (no of columns) + j] b[1][j] is same as c[1 * (no of columns) + j] and in general: b[i][j] is same as c[i * (no of columns) + j]

Pixel (picture element): 4 components, each in 0..255 Contains: alpha component (we never change it) red component r green component g blue component b /** DM provides functions for extracting components of a pixel. */ public static final DirectColorModel DM= (DirectColorModel) ColorModel.getRGBdefault(); Procedure invert has in it: DM.getRed(pixel); alpha red green blue all 4 components fit into an int 8 bits 8 bits 8 bits 8 bits (alpha << 24) | (red << 16) | (green << 8) | blue)

contains red, white, blue balls Dutch National Flag pre b ? 0 n contains red, white, blue balls Permute b[0..n-1] to truthify: post b reds whites blues 0 n Generalize the pre- and post-condition to get a loop invariant.

Partition algorithm: Given an array b[h..k] with some value x in b[h]: P: b Swap elements of b[h..k] and store in j to truthify P: <= x x >= x h j k Q: b 3 5 4 1 6 2 3 8 1 b h k change: into 1 2 1 3 5 4 6 3 8 h j k 1 2 3 1 3 4 5 6 8 b h j k or x is called the pivot value. x is not a program variable; x just denotes the value initially in b[h].

Partition algorithm: Given an array b[h..k] with some value x in b[h]: P: b Swap elements of b[h..k] and store in j to truthify Q: <= x x >= x h j k Q: b You generalize P and Q to create a loop invariant x is called the pivot value. x is not a program variable; x just denotes the value initially in b[h].

Binary search: Vague spec: Look for v in sorted array segment b[h..k]. 6 16 1 2 2 2 3 6 6 9 9 9 9 h k b h 6 k 16 Store the index of v in variable i i v 1 i v i v 12 i v 2 i v 4

Binary search: Vague spec: Look for v in sorted array segment b[h..k]. Precondition P: b[h..k] is sorted (in ascending order). Store in i to truthify: Postcondition Q: b[h..i] <= v and v < b[i+1..k] Below, the array is in non-descending order: ? h k P: b Called binary search because each iteration of the loop cuts the array segment still to be processed in half <= v > v h i k Q: b

Invariant as picture: Generalizing pre- and post-condition Finding the minimum of an array. Given array b satisfying precondition P, store a value in x to truthify postcondition Q: ? and n >= 0 0 n P: b (values in 0..n are unknown) x is the min of this segment 0 n Q: b The invariant combines P and Q

OR Linear search Vague spec.: Find first occurrence of v in b[h..k-1]. Better spec.: Store an integer in i to truthify postcondition Q: Q: 1. v is not in b[h..i-1] 2. i = k OR v = b[k] v is in here h k P: b x not here x ? h i k Q: b x not here i h k OR b

Change: into Reversal: Reverse the elements of array segment b[h..k]. precondition P: not reversed h k postcondition Q: reversed 1 2 3 4 5 6 7 8 9 9 9 9 b h k Change: into 9 9 9 9 8 7 6 5 4 3 2 1 b h k