Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Analysis of Algorithms II
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.
CS 400/600 – Data Structures External Sorting.
COSC 2006 Data Structures I Recursion III
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CMPS1371 Introduction to Computing for Engineers SORTING.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 20: Sorting.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
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.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
External Sorting Problem: Sorting data sets too large to fit into main memory. –Assume data are stored on disk drive. To sort, portions of the data must.
Advanced Counting Techniques
(c) , University of Washington
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Internal and External Sorting External Searching
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Sort Algorithm.
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Sorting Algorithms.
CSC 421: Algorithm Design & Analysis
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Sorting Algorithms Written by J.J. Shepherd.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Fundamentals of Programming
Data Structures Review Session
8/04/2009 Many thanks to David Sun for some of the included slides!
Divide and Conquer Algorithms Part I
Sorting "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.
CSC 421: Algorithm Design & Analysis
Presentation transcript:

Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your neighbor do most of the work. Your neighbor is nearly as smart as you, and can do what you can do – almost, so the problem that you give your name has to be (at least) a little smaller than the problem that you have to solve.

Example Suppose that you are asked to sort a list of 10 numbers. Instead of doing all the work, you just take the first number off the list, and then pass the buck to your neighbor – you ask your neighbor to sort the remaining list of 9 numbers. When your neighbor gives you his or her answer, you just have to insert that first number back into the right place in the list and you're done.

Pass the Buck (cont'd) You pass the buck to your neighbor. Your neighbor is also lazy, and will try to do as little work as possible by playing Pass the Buck. So, your neighbor, in this example, will take the first number from the list and then pass the buck to his or her neighbor, who does the same thing. And so on and so on, until...

The Idiot Case The buck is passed until the problem is so simple, that any idiot could solve it (the idiot case.) In our example, this would be the list with just one number. So, the last person – the idiot – just solves the problem directly, and returns the answer to the neighbor who gave it to him or her. That person finds the solution to their problem, and returns it to their neighbor who gave them the problem...

Example In the problem is to sort a list of five numbers: Person 1 takes 7 from the list and asks Person 2 to sort the list Person 2 takes 1 from the list and ask Person 3 to sort Person 3 takes 3 from the list and asks Person 4 to sort 4 6. Person 4 takes 6 from the list and asks Person 5 to sort the list 4. This is the idiot case, so Person 6 returns 4.

Example (cont'd) Person 4 then adds 6 in the proper place and gives back the list 4 6. Person 3 adds 3 back to the list, giving Person 2 adds 1 to the list giving , and Person 1 adds 7 back in, which yields , the correct answer.

Another Example Suppose instead of just taking the first number from the list, Person 1 (and hence everyone else, since they all should do the same thing except for the idiot), finds the smallest number from the list and removes that number. Then, after passing the buck and getting back the list from Person 2, Person 1 just has to stick the smallest number to the beginning of the list to sort the whole list.

Sorting The first example is actually insertion sort, and then second example is selection sort. Is this just another way of writing the sort, or is it more efficient? Each person does a little bit of work, but how much work does everyone do put together? In the first example, each person (except for the idiot), removes the first number which takes constant time, and then later has to put the number back in the right place.

Sorting (cont'd) This takes N-1 comparisons if the list has N numbers. So, Person 1 does 5 – 1 comparisions, Person 2, does 4 – 1 comparisons, and so on...until the idiot who does no comparisons. In general, given a list of size N, Person 1 does N-1 comparisons, Person 2 does N- 2 comparisons...down to 1, then 0 comparison, for a total of N*(N-1)/2 comparisons which is the same as insertion sort.

Other Sorting In the second example, Person 1 finds the smallest number first which takes N-1 comparisons. To finish up the job, Person 1 just sticks the number back at the beginning of the list which is constant time. So, again, the total number of comparisons is N * (N-1)/2 which is the same as selection sort.

The Rules of Pass the Buck Given a problem of size N, you must find a smaller problem of the same type, and give that problem to your neighbor. Your neighbor solves his or her problem and gives you back the solution. You must then extend that solution to be a solution to the original problem. Since your neighbor also passes the back, the problem keeps getting smaller and smaller until any idiot can do it. The idiot solves his or her problem directly and returns the answer.

Tower of Hanoi Problem An example where it's easier to play Pass the Buck then to solve a problem directly is the Tower of Hanoi problem. The puzzle has three wooden pegs. On one peg are place a number of disks of various sizes, with the biggest disk on the bottom and then disks getting smaller as you go up. The problem is to move the stack of disks from the first peg to the third peg.

Tower of Hanoi (cont'd) However, you can only move one disk at a time, and you may not put a larger disk on top of a smaller disk, i.e., a disk can only be moved to an empty peg, or onto a larger disk.

Solution If your problem is to move N disks from peg 1 to peg 3, then you pass the buck by asking your neighbor to move N-1 disks (the top N-1 disks) from peg 1 to peg 2 (the spare peg). Then you move the bottom disk from peg 1 to peg 3. Finally you ask your neighbor to move the stack of N-1 disks from peg 2 to peg 3, completing the entire task. The idiot case is when there is just one disk in the stack – just move the disk directly.

Sorting Redux Another way to sort is to make the list smaller by more than one element. Split the list in half – have a neighbor sort the first half, and then have a neighbor sort the second half. Then you must merge the two sorted lists into one sorted list. Merging takes N-1 comparisons (at worst), so it is simpler than sorting.

Mergesort The idiot case is again, when there is just one number in the list or the list is empty. In both cases, there's nothing to do, just return the list. The first person does N-1 comparisons for the merge step - the split is constant. The second person has to sort a list of size N/2, which takes N/2-1 comparisons, but is asked to do that twice. The third person sorts 4 lists, of size N/4, and so on. The total number of comparison is N * log 2 (N).

Another Sort Another way to split the list of numbers is into a list of small numbers and a list of large numbers. For this example, we will use the first number of the list to decide which numbers are large and which numbers are small. Numbers less than the first number are considered small, and numbers greater than the first number are considered large. We assume no duplicate numbers in this example.

Another Sort (cont'd) So, the first person removes the first number from the list, and then using that number splits the list into two (not necessarily equal) pieces – the numbers that are less than the first number and the numbers that are greater than the first number. The buck is passed (twice) and both sub-lists are sorted. The first person just puts the two lists together with the first number in between them.

How much work is done? It takes linear time to split the list into the two pieces, but only constant time to put the lists back together (assuming that we're working with arrays and keeping the numbers in place). However, since the list isn't split in half, we can't assume that the two lists are size N/2. If we're lucky, and they are, then the time is the same as mergesort, i.e., N log 2 (N).

Worst Case In the worst case, the first number is the smallest number in the list (or the largest) and then all the rest of the numbers fall in the list of large numbers with none in the small number list. So, the buck is passed twice, once with the empty list (an idiot case) and once with a list of size N-1. This becomes (if repeated) the same as insertion sort, i.e., N*(N-1)/2.