CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

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.
Introduction to Algorithms Quicksort
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.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
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;
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.
CS180 RECURSION March 28,2008. Announcements Project 7 : Recursive Expression Evaluators Milestone Due : 4/2/2008 Project Due : 4/9/2008 Exam 2 to be.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Recursion CITS1001.
Sorting and Asymptotic Complexity
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 CS April 2010 binary search, linear search insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CS 100Lecture 231 Announcements Check your grades on the door of 5141 Upson More review tomorrow Review session Sunday night w/Alan FINAL EXAM: Tuesday.
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.
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.
Searching and Sorting Searching algorithms with simple arrays
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Lecture 14 Searching and Sorting Richard Gesick.
Data Structures in Java with JUnit ©Rick Mercer
Recursion (Continued)
Week 12 - Wednesday CS221.
Announcements Final Exam on August 17th Wednesday at 16:00.
Quicksort 1.
Algorithm design and Analysis
Announcements P2 is due tomorrow Prelim on Monday
Lecture 11 Searching and Sorting Richard Gesick.
"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
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
"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
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
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
Lecture 20 – Practice Exercises 4
Presentation transcript:

CS 100Lecture 131 Announcements Exam stats P3 due on Thursday

CS 100Lecture 132 Today’s Topics Discussion of Prelim Review of sorting algorithms from last week Iterative Quicksort Tomorrow: Matlab begins

CS 100Lecture 133 Prelim Highlights Classes/Subclasses/Inheritance Method invocations b1 = true; b2 = false; b1 = b1 && b2 means that now b1 is false

CS 100Lecture 134 Question 1 A) double[] b = { 5.0, 6.2, 3.7 } ; B) if (x > y) min = x; else min = y; C) Binary search requires sorted input, linear search does not. Binary search is much faster than linear search.

CS 100Lecture 135 Question 2 Remember how aliases work, when an assignment like c1 = c2; occurs. Output of the code is:

CS 100Lecture 136 Question 3 public class geometricObject { public int numSides; // number of sides of the object private String color; // color of the object public geometricObject (int sides, String c) { numSides = sides; color = c; } public String getColor () { return color; }

CS 100Lecture 137 Question 3, continued public class Rectangle extends geometricObject{ public int length; // length of the rectangle public int width; // width of the rectangle public Rectangle(int userLength, int userWidth, String color){ super(4, color); length = userLength; width = userWidth; } public int area() { return length*width;} } public class Square extends Rectangle { public Square(int side, String color) { super(side, side, color); }

CS 100Lecture 138 Question 4 public int numLess(int []b, int x) { int i = 0; int num = 0; while (i < b.length) { if (b[i] <= x) num++; i++; } return num; }

CS 100Lecture 139 Question 5 Allocate a new set of locations to contain the parameters and local variables of the method being called, in what we call a frame. Assign the arguments of the call to the parameters of the method. Execute the procedure body. Delete the frame produced in step 1; if the method is a function return the value of the function to the place of the call.

CS 100Lecture 1310 Question 5, continued b c 3 x 4 y Box for the class C An instance of C 10 y w ______ z ___4___ ourMethod C C

CS 100Lecture 1311 Question 6 public static void main (String args[]) { ATM my_atm = new ATM(); my_atm.balance = 250; my_atm.printBalance(); my_atm.withdraw(200); my_atm.printBalance(); }

CS 100Lecture 1312 Review of quicksort Given an array b[0..k] –if b.length = 1, done –if b.length = 2, then swap if necessary, done. –partition the array around b[0], suppose value in b[0] ends up in position j –quicksort b[0..j-1] –quicksort b[j+1..k] This version of quicksort is recursive (calls itself)

CS 100Lecture 1313 Iterative quicksort (no recursion) After partitioning the array, it looks like: There are now two sections to sort, b[h..j-1] and b[j+1..k], and while one is being sorted, it must be remembered to sort the other. Sorting b[h..j-1] will result in partitioning and the creation of two other sections to sort; these must also be “remembered”. x h j k b

CS 100Lecture 1314 Create a class Bounds // An instance represents the bound f and l of an // array section b[f..l] (for some array) public class Bounds { public int f; public int l; // Constructor: instance with f=fp and l=lp public Bounds(int fp, int lp) {f= fp; l= lp;} }

CS 100Lecture 1315 public void Quicksort(int [ ] b, int h, int k) { Bounds c [ ] = new Bounds [ k+1-h]; System.out.println(k); System.out.println(h); c[0]= new Bounds(h,k); int i= 1; // inv: b[h..k] is sorted iff all its subsegments // defined by elements of c[0..i-1] are sorted while (i > 0) { i = i -1; int f= c[i].f; int l= c[i].l; // Process segment b[f..l]

CS 100Lecture 1316 if (l-f==1) {// b[f..l] has two elements if (b[f] > b[l]) { // Swap b[f] and b[l] int t= b[f]; b[f]= b[l]; b[l]= t; }} else if (l-f>1) { //b[f..l] has > 2 elements // Add bounds of b[f..j-1] and b[j+1..k] to c int j= partition(b,f,l); c[i]= new Bounds(f,j-1); i= i+1; c[i]= new Bounds(j+1,l); i= i+1;}}}

CS 100Lecture 1317 Size of array c How big can array c get? Let b[h..k] have n values, and let b be already sorted. At each step, b[f..j-1] would be empty and b[j+1..k]would have all but one of the elements. After 3 loop iterations, we would have c[0] represents a segment of 0 elements c[1] represents a segment of 0 elements c[2] represents a segment of 0 elements c[3] represents a segment of n-3 elements In worst case array c needs almost n array elements!

CS 100Lecture 1318 How to fix this... Put largest of the two segments b[f..j-1], b[j+1..k] on c first, then the smaller. Then, we can show that that if c[0] represents a segment of m elements, c looks like c[0] represents m elements c[1] represents < m/2 elements c[2] represents < m/4 elements c[3] represents < m/8 elements … c[i-1] represents m/ 2 i-1 elements c has at most 1+ log m elements So c has at most 1 + log n elements. Much better!

CS 100Lecture 1319 Changes to algorithm Changes to ensure that array c never gets bigger than log (l-f). If the array has 2 50 elements, array c need have no more than 50 elements. 1. Change allocation of c to Bounds c [ ] = new Bounds [ 50]; 2. Change implementation of “Add bounds …” to the following:

CS 100Lecture 1320 Code Modifications // Add bounds of b[f..j-1] and b[j+1..k] to c // --put larger segment on first if (j-f > l-j) { c[i]= new Bounds (f,j-1); i= i+1; c[i+1]= new Bounds(j+1..k); i= i+1; } else { c[i]= new Bounds (j+1..k); i= i+1; c[i]= new Bounds(f,j-1); i= i+1; }