Programming Abstractions

Slides:



Advertisements
Similar presentations
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
Advertisements

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 373: Data Structures and Algorithms
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
(c) , University of Washington
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Sorting! 1.The warm-ups  Selection sort  Insertion sort 2.Let’s use a data structure! 
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Programming Abstractions Cynthia Lee CS106X. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism › Example: Expression.
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.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
Programming Abstractions Cynthia Lee CS106B. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
Programming Abstractions
Fundamental Data Structures and Algorithms
Programming Abstractions
CSCI 104 Sorting Algorithms
Searching – Linear and Binary Searches
Sorting Why? Displaying in order Faster Searching Categories Internal
Programming Abstractions
Warmup What is an abstract class?
October 30th – Priority QUeues
Chapter 7 Sorting Spring 14
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Week 12 - Wednesday CS221.
Data Structures and Algorithms
Topic 14 Searching and Simple Sorts
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Selection Sort Sorted Unsorted Swap
CSC212 Data Structure - Section RS
Unit-2 Divide and Conquer
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
8/04/2009 Many thanks to David Sun for some of the included slides!
IT 4043 Data Structures and Algorithms
CS200: Algorithms Analysis
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
Topic 14 Searching and Simple Sorts
Searching.
CSE 326: Data Structures Sorting
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSC 143 Java Sorting.
Sorting Chapter 10.
CSE 373 Data Structures and Algorithms
Searching/Sorting/Searching
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSE 373: Data Structures and Algorithms
CSE 332: Sorting II Spring 2016.
CSCE 3110 Data Structures & Algorithm Analysis
CS203 Lecture 15.
Programming Abstractions in C++, Section 10.2
Module 8 – Searching & Sorting Algorithms
CSE 373: Data Structures and Algorithms
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Today’s Topics (first, finish up Inheritance/Polymorphism) Sorting! The warm-ups Selection sort Insertion sort Let’s use a data structure! Heapsort Divide & Conquer Merge Sort (aka Professor Sorting the Midterms Using TAs and SLs Sort) Quicksort

Polymorphism examples You can use the object's extra functionality by casting. Employee *diane = new Lawyer("Diane", "Stanford", 5); diane->vacationDays(); // ok diane->sue("Cynthia"); // compiler error ((Lawyer*) diane)->sue("Cynthia"); // ok Pro Tip: you should not cast a pointer into something that it is not! It will compile, but the code will crash (or behave unpredictably) when you try to run it. Employee *carlos = new Programmer("Carlos", 3); carlos->code(); // compiler error ((Programmer*) carlos)->code("C++"); // ok ((Lawyer*) carlos)->sue("Cynthia"); // No!!! Compiles but crash!!

Rules for “virtual”: runtime calls DerivedType * obj = new DerivedType(); If we call a method like this: obj->method(), only one thing could happen: DerivedType’s implementation of method is called BaseType * obj = new DerivedType(); If we call a method like this: obj->method(), two different things could happen: If method is not virtual, then BaseType’s implementation of method is called If method is virtual, then DerivedType’s implementation of method is called

Rules for “virtual”: pure virtual If a method of a class looks like this: virtual returntype method() = 0; then this method is a called “pure virtual” function and the class is called an “abstract class” Abstract classes are like Java interfaces You cannot do “= new Foo();” if Foo is abstract (just like Java interfaces) ALSO, you cannot do “= new DerivedFoo();” if DerivedFoo extends Foo and DerivedFoo does not implement all the pure virtual methods of Foo

Gives an error (identify compiler or crash) Other/none/more class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Siamese * s = new Mammal; cout << s->toString(); “Mammal” “Cat” “Siamese” Gives an error (identify compiler or crash) Other/none/more

Gives an error (identify compiler or crash) Other/none/more class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Siamese * s = new Siamese; cout << s->toString(); “Mammal” “Cat” “Siamese” Gives an error (identify compiler or crash) Other/none/more

Gives an error (identify compiler or crash) Other/none/more class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Mammal * m = new Mammal; cout << m->toString(); “Mammal” “Cat” “Siamese” Gives an error (identify compiler or crash) Other/none/more

Gives an error (identify compiler or crash) Other/none/more class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Mammal * m = new Siamese; cout << m->toString(); “Mammal” “Cat” “Siamese” Gives an error (identify compiler or crash) Other/none/more

Gives an error (identify compiler or crash) Other/none/more class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Mammal * m = new Siamese; m->scratchCouch(); “Mammal” “Cat” “Siamese” Gives an error (identify compiler or crash) Other/none/more

Gives an error (identify compiler or crash) Other/none/more class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Cat * c = new Siamese; c->makeSound(); “rawr” “meow” “Siamese” Gives an error (identify compiler or crash) Other/none/more

Selection Sort A classic “My First Sorting Algorithm” sorting algorithm

Selection Sort void sort(Vector<int> &vec) { int n = vec.size(); // already-fully-sorted section grows // 1 at a time from left to right for (int lh = 0; lh < n; lh++) { int rh = lh; // find the min element in the // entire unsorted section for (int i = lh + 1; i < n; i++) { // found new min? if (vec[i] < vec[rh]) rh = i; } // swap min into sorted section int tmp = vec[lh]; vec[lh] = vec[rh]; vec[rh] = tmp; Compare the best-case and worst-case costs of this algorithm (tight Big-O characterization of each): Best case = Worst case Best case < Worst case Why? Explain very specifically.

Bubble Sort It’s not very good, famously so… https://www.youtube.com/watch?v=k4RRi_ntQc8 (arguably better than Selection Sort though!)

Insertion Sort Another classic “Beginner” sorting algorithm

Insertion Sort void sort(Vector<int> & vec) { int n = vec.size(); // already-sorted section grows 1 at a // time from left to right for (int i = 1; i < n; i++) { int j = i; // does this item needs to move // left to be in order? while (j > 0 && vec[j-1] > vec[j]) { // keep swapping this item with // its left neighbor if it is // smaller than the left neighbor int tmp = vec[i]; vec[i] = vec[j]; vec[j] = tmp; j--; } Compare the best-case and worst-case costs of this algorithm (tight Big-O characterization of each): Best case = Worst case Best case < Worst case Why? Explain very specifically.

Heap Sort

Heapsort Pretty simple!! Take the unsorted array and insert each element into a heap priority queue While the queue is not empty, dequeue an element from the heap priority queue The elements come out of the priority queue in sorted order. Fun fact: you don’t need extra array storage, you can do this in place in the original array.

Professor’s Sorting Algorithm Sorting in the “real world”

Preliminary Step: We need a “combine two sorted piles” algorithm Start: you have two piles, each of which is sorted Take the overall smallest element (smallest in either pile) and add that one element to the combined-sorted pile Repeat until the two starting piles are empty and the combined-sorted pile is complete Towards the end, you might end up with one pile already empty and the other not, so just move from non-empty pile into combined-sorted pile

Preliminary Step: We need a “combine two sorted piles” algorithm Start: you have two piles, each of which is sorted Take the overall smallest element (smallest in either pile) and add that one element to the combined-sorted pile Repeat until the two starting piles are empty and the combined-sorted pile is complete Towards the end, you might end up with one pile already empty and the other not, so just move from non-empty pile into combined-sorted pile How many elements do we examine to find the overall smallest element?

How many steps does it take to merge two sorted sub-piles, A and B How many steps does it take to merge two sorted sub-piles, A and B? In other words, how long does it take to do the “combine two sorted piles” algorithm on piles A and B? (best/tight answer) O(log(|A|+|B|)) steps O(|A|+|B|) steps O(|A+B|)2 steps O(|A|2 + |B|2)steps Other/none/more than one

Professor’s sorting algorithm: Stanford CS classes can have more than 500 students! Sorting the midterms alphabetically to prepare for handing them back is a non-trivial task. Luckily, I don’t have to do it myself… Find two grad students, give each half of the unsorted midterms Tell the grad students to sort their own pile, then give it back Combine the two piles into one sorted pile, using our simple combine algorithm Done!

Grad student’s sorting algorithm: Sorting ~250 exams is still a non-trivial task! Luckily, the grad students don’t have to do it themselves! Find two SLs, give each half of the unsorted midterms Tell the SLs to sort their own pile, then give it back to you Combine the two piles into one sorted pile, using our simple combine algorithm Done! (give your sorted pile to professor)

SL’s sorting algorithm: Find two students, give each half of the unsorted midterms Tell the students to sort their own pile, then give it back to you Combine the two piles into one sorted pile, using our simple combine algorithm Done! (give sorted pile to grad student)

Student’s sorting algorithm: Find two visiting prospective freshmen (“profros”), give each half of the unsorted midterms Tell the profros to sort their own pile, then give it back to you Combine the two piles into one sorted pile, using our simple combine algorithm Done! (give sorted pile to SL)

ProFro’s sorting algorithm: By now, the pile only has zero or one exam in it (for the sake of this example, assume the starting number of exams makes this true at this point) Done! (give sorted pile to student)

Other/none/more than one Consider an arbitrarily chosen (generic) particular exam and mentally track its progress throughout the algorithm. How many times does your exam pass through the merge algorithm? 1 time 2 times Θ(logn) times Θ(n) times Other/none/more than one (Recall Θ means the same as O but where the time is a best match, not a potentially distant upper bound.)

BigO Analysis of Mergesort Every paper is merged log(n) times This is the number of times we can divide the stack of n papers by 2 before we can’t divide anymore There are n papers O(nlogn)

Merge Sort runtime intuition Merge sort performs O(N) operations on each level. (width) Each level splits the data in 2, so there are log2 N levels. (height) Product of these = N * log2 N = O(N log N). (area) Example: N = 32. Performs ~ log2 32 = 5 levels of N operations each: height = log2 N 32 16 8 4 2 1 width = N

Merge Sort Compare the best case and worst case of Merge sort (tight Big-O of each): Best case = Worst case Best case < Worst case Why? Explain very specifically in terms of the structure of the code.

Quicksort

Divide & Conquer Imagine we want students to line up in alphabetical order to pick up their midterms, which (as we know from Professor sorting algorithm!) are sorted in alphabetical order. “Everybody in the first half of the alphabet, go over there!” “Everybody in the second half, go over there!” At this point, we at least have some kind of division based on ordering, but it’s very crude. Each of the two “over there” groups is completely unsorted within the group, but... …at least now you have two groups that are each smaller and easier to sort, so recursively sort each half. That’s it!* * ok, actually there are some details…

Divide & Conquer Imagine we want students to line up in alphabetical order to pick up their midterms, which (as we know from Professor sorting algorithm!) are sorted in alphabetical order. “Everybody in the first half of the alphabet, go over there!” “Everybody in the second half, go over there!” At this point, we at least have some kind of division based on ordering, but it’s very crude. Each of the two “over there” groups is completely unsorted within the group, but... …at least now you have two groups that are each smaller and easier to sort, so recursively sort each half. That’s it!* * ok, actually there are some details… Rather than doing the work of finding the actual median, we just choose an arbitrary or random element to be the divider. Say, the first array index of the group, or randomly select an array index from the group.

Quicksort Consider the best case and worst case of Quicksort (best/tight characterization in each case) Best case = Worst case Best case < Worst case Why? Explain very specifically in terms of the structure of the code.