1 CS1110 2 April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp. 311-312 to get familiar with concepts and develop skill.

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

MATH 224 – Discrete Mathematics
Garfield AP Computer Science
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Recitation on analysis of algorithms. runtimeof MergeSort /** Sort b[h..k]. */ public static void mS(Comparable[] b, int h, int k) { if (h >= k) return;
Software Engineering and Design Principles Chapter 1.
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.
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 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
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 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Asymptotic Complexity
Abstract Data Types (ADTs) Data Structures The Java Collections API
(c) , University of Washington
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Problem Solving and Algorithms
Recursive Quicksort Data Structures in Java with JUnit ©Rick Mercer.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
1 Searching and Sorting Linear Search Binary Search.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Recitation on analysis of algorithms. Formal definition of O(n) We give a formal definition and show how it is used: f(n) is O(g(n)) iff There is a positive.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
while there is room A draws or ; B draws or ; A wants to get a solid closed curve. B wants to stop A from getting a solid closed curve. Who.
1 CS November 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
1 CS April 2010 binary search, linear search insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Exam 2 Review CS 3358 Data Structures. 90 Total Points – 50 Points Writing Programs – 25 Points Tracing Algorithms, determining results, and drawing pictures.
1 CS November 2008 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
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.
Correctness issues and Loop invariants
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
"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
Search,Sort,Recursion.
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
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
CS100J Nov 04, 2003 Arrays. Reading: 8
Developing Loops from Invariants
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

1 CS April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice in DrJava! Test your methods! Course website contains more prelims and answers, for Prelim 1, 2, and 3 Time spent on A5: min 2 (11 students) average 5.7 median5 max15 15 students took 9-14 hours 26 students took 7-8 hours

2 Comments on A5 Recursion: Make requirements/descriptions less ambiguous, clearer; give more direction. Should be a part where you can create your own recursive functions Need optional problem with more complicated recursive solution would have been an interesting challenge, more recursive functions. Do more of recursive graphics-type problems. They really make us think! Make task 5 easier. I could not finish it. Include a clue that more than one recursive call can be included in a recursive method. I wasted hours because I didn't know this. Clarify clear(), setPanel- Size(). Improve graphic rendering. Explain diff between JFrame and JPanel Liked not having to write test cases! Needed too much help, took too long Add more methods; it did not take long Give us the option to do the recursive methods with loops rather than recursively.

3 Sorting: ? 0 n pre: b sorted 0 n post: b sorted ? 0 i ninsertion sort inv: b for (int i= 0; i < n; i= i+1) { } “sorted” means in ascending order i i Push b[i] down into its sorted position in b[0..i]; Iteration i makes up to i swaps. In worst case, number of swaps needed is … (n-1) = (n-1)*n / 2. Called an “n-squared”, or n 2, algorithm. b[0..i-1]: i elements in worst case: Iteration 0: 0 swaps Iteration 1: 1 swap Iteration 2: 2 swaps …

4 ? 0n0n pre: b sorted 0n0n post: b Add property to invariant: first segment contains smaller values. ≤ b[i..], sorted ≥ b[0..i-1], ? 0 i n invariant: b selection sort sorted ? 0 i n invariant: b insertion sort for (int i= 0; i < n; i= i+1) { } i n i n 7 int j= index of min of b[i..n-1]; Swap b[j] and b[i]; Also an “n-squared”, or n 2, algorithm.

5 Please put away your books and get ready for a quiz —get out a blank piece of paper.

6 1. Write your name and Cornell NetId –legibly. 2. Specify the binary search algorithm –you can do this by drawing the pre- and post-conditions as diagrams. 3. Develop the loop invariant from the pre- and post- conditions. 4. Develop the algorithm, using the four loopy questions.

7 Quicksort /** Sort b[h..k] */ public static void qsort(int[] b, int h, int k) { } if (b[h..k] has fewer than 2 elements) return; j= partition(b, h, k); = x h j k post: b x ? h k pre: b int j= partition(b, h, k); // b[h..j–1] <= b[j] <= b[j+1..k] // Sort b[h..j–1] and b[j+1..k] qsort(b, h, j–1); qsort(b, j+1, k); To sort array of size n. e.g Worst case: n 2 e.g Average case: n log n. e.g. 15 * = 32768

8 Tony Hoare, in 1968 Quicksort author Tony Hoare in 2007 in Germany Thought of Quicksort in ~1958. Tried to explain it to a colleague, but couldn’t. Few months later: he saw a draft of the definition of the language Algol 58 –later turned into Algol 60. It had recursion. He went and explained Quicksort to his colleague, using recursion, who now understood it.

Viewpoint On teaching programming Reply 9 I don't like how we are forced to visualize things in Dr. Gries' way. … Entire point of programming is to be able to look at things in different ways and come up with different solutions for one problem. Forcing us to think of things in his way and testing us on it has been detrimental to my learn- ing because in my opinion it wastes time and confuses me. This course should focus more on solving problems rather than drawing folders to represent objects. 1. A model of execution of Java programs is needed in order to bring understanding. 2. Problem solving is the focus. The programs you wrote for A5, the algorithms we are now studying, and the way we develop them, could not have been possible without the basics that we have given you. 3. We are giving you tools for coming up with good solutions, not just different ones.

The NATO Software Engineering Conferences homepages.cs.ncl.ac.uk/brian.randell/NATO/ 7-11 Oct 1968, Garmisch, Germany Oct 1969, Rome, Italy Download Proceedings, which have transcripts of discussions. See photographs. Software crisis: Academic and industrial people. Admitted for first time that they did not know how to develop software efficiently and effectively.

11 Software Engineering, 1968 Next years: intense period of research of software engineering, language design, proving programs correct, etc.

12 Software Engineering, 1968

During 1970s, 1980s, intense research on How to prove programs correct, How to make it practical, Methodology for developing algorithms 13 The way we understand recursive methods is based on that methodology. Our understanding of and development of loops is based on that methodology. Throughout, we try to give you thought habits to help you solve programming problems for effectively Mark Twain: Nothing needs changing so much as the habits of others.

14 The way we understand recursive methods is based on that methodology. Our understanding of and development of loops is based on that methodology. Throughout, we try to give you thought habits to help you solve programming problems for effectively Simplicity is key: Learn not only to simplify, learn not to complify. Separate concerns, and focus on one at a time. Don’t solve a problem until you know what the problem is (give precise and thorough specs). Develop and test incrementally. Learn to read a program at different levels of abstraction.