1 CS1110 7 April 2010 insertion sort, selection sort, quick sort Do exercises on pp. 311-312 to get familiar with concepts and develop skill. Practice.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

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.
MATH 224 – Discrete Mathematics
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
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.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
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.
Quicksort.
Quicksort
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 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Sorting and Asymptotic Complexity
Recursive Quicksort Data Structures in Java with JUnit ©Rick Mercer.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Computer Science Searching & Sorting.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
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.
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.
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.
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.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
1 CS November 2008 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
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 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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Data Structures in Java with JUnit ©Rick Mercer
Quicksort 1.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
"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
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.
CSE 373 Data Structures and Algorithms
Binary Search and Loop invariants
Asymptotic complexity
CS November 2010 Developing array algorithms. Reading:
Sorting and Asymptotic Complexity
Quicksort.
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
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice in DrJava! Test your methods! A5 times median 6.5 mean : : : 4

2 Comments on A5 Recursion: Make requirements/descriptions less ambiguous, clearer; give more direction. Need optional problem with more complicated recursive solution would have been an interesting challenge, more recursive functions. They make us think! Make task 5 easier. I could not finish it. Liked not having to write test cases! Needed too much help, took too long Add more methods; it did not take long Allow us to do recursive methods with loops rather than recursively. Good time drinking beer while watching the demo after I was done. I had intended here to erupt in largely incoherent rage over that wretched concept of recursion, which I came to hate like an enemy: like a sentient being who, knowing the difference between right and wrong, had purposely chosen to do me harm. However, I then figured out how it works, and it is actually quite elegant, so now I suppose I have learned something against my will.

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 Draw a diagram here to show what the array has to look like before i can be incremented so that inv is true after incrementing i State in English what you did. Don’t mention loops or anything like that i i

4 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 …

5 ? 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 Draw diagram to show what array has to look like so that incrementing i makes inv true i n i n State in English what you did.

6 ? 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.

7 Partition algorithm: Given an array b[h..k] with some value x in b[h]: x ? h k P: b = x h j k Q: b b h k change: into b h j k x is called the pivot value. x is not a program variable; x just denotes the value initially in b[h]. Swap elements of b[h..k] and store in j to truthify P: b h j k or

8 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); h j k Write a diagram that shows this arrays sorted Write down in English what you did

9 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

10 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.

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.

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

13 Software Engineering, 1968

During 1970s, 1980s, intense research on How to prove programs correct, How to make it practical, Methodology for developing algorithms 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 Mark Twain: Nothing needs changing so much as the habits of others.

15 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.