Mergesort: The power of divide and conquer

Slides:



Advertisements
Similar presentations
Spit-not-so Prof Paul Curzon Queen Mary University of London With support from Google, D of E.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
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.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CHAPTER 11 Sorting.
Sorting Chapter 10.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Soda Constructor: Exploring the laws of Physics with Computational Thinking Paul Curzon Queen Mary University of London
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Make-a-face Prof Paul Curzon Queen Mary, University of London With support from Google, D of.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
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.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Hexahexaflexagon Automata Paul Curzon Queen Mary University of London With support from Google,
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Explorers need maps: Abstraction, representations and graphs Paul Curzon Queen Mary University of London
The Imp Computer Prof Paul Curzon Queen Mary, University of London With support from Google,
 initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.
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.
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)
 initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Box Variables Prof Paul Curzon Queen Mary, University of London With support from Google, D of.
Lists and Sorting Algorithms
Advanced Sorting.
Prof. U V THETE Dept. of Computer Science YMA
Sorting.
Sorts, CompareTo Method and Strings
Searching and Sorting Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Algorithm Efficiency and Sorting
CS 162 Intro to Programming II
Warmup What is an abstract class?
Chapter 7 Sorting Spring 14
Teach A level Computing: Algorithms and Data Structures
Divide and Conquer.
Punch Card Sorting: Binary Radix Sort
Bubble Sort Paul Curzon
Algorithm Design Methods
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
The intelligent piece of paper: so what is an algorithm?
Bakuro: Binary Logical Thinking Puzzles
The SwapPuzzle So what is an algorithm?
Unit-2 Divide and Conquer
“Human Sorting” It’s a “Problem Solving” game:
Sodarace: Exploring Evolution with Computational Thinking
Sorting Algorithms Ellysa N. Kosinaya.
Queen Mary University of London
8/04/2009 Many thanks to David Sun for some of the included slides!
Yan Shi CS/SE 2630 Lecture Notes
Chapter 4.
Algorithm Efficiency and Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1114: Sorting and selection (part two)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
“Human Sorting” It’s a “Problem Solving” game:
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

Mergesort: The power of divide and conquer Paul Curzon Queen Mary University of London CAS London With support from Google, Dept for Education the Mayor of London www.teachinglondoncomputing.org Twitter: @TeachingLDNComp

Aims www.teachinglondoncomputing.org Give you deeper understanding of core topics Merge sort Divide and Conquer Efficiency of algorithms Computational thinking Give you practical ways to teach computing in a fun, thought provoking way away from computers, focus on concepts Linked activity sheets and booklets can be downloaded from our website: www.teachinglondoncomputing.org

Sort Algorithms www.teachinglondoncomputing.org A sort algorithm takes an array of data and puts it into order (either ascending order or descending order) eg [5, 7, 2, 99, 4] -> [2, 4, 5, 7, 99] [“cat”, “hat”, “ant”] -> [“ant”, “cat”, “hat”] Often used as a way of making things easier to find (eg in a telephone directory) There are many sort algorithms some more efficient than others www.teachinglondoncomputing.org

Divide and Conquer Sorting Suppose you have a long array that you want to sort. Divide and conquer says: split it into smaller arrays sort those smaller arrays (in the same way) combine the sorted arrays to give the sorted large array. There are two approaches split is easy but combining them at the end needs work split takes time but combining them at the end is easy

1 Mergesort To mergesort an array: split the array in half mergesort those smaller arrays merge the two sorted arrays Note it does option 1 - split is easy but combining them at the end needs work We will act it out Each new call of merge sort will be done by a new person 1

1 The Merge Algorithm Merge is the key to this as it does all the work To Merge two sorted piles Repeat until both piles empty Compare the top value of each pile Take the smallest 1

How much work? How many comparisons do we need to do to guarantee it is sorted? What is the worst situation we could be in? What is the best? 1

1 How much work? Each time we recurse we go back a row of people Each run of merge (so each person) does approximately one comparison for every piece of data being merged. When we mergesort 32 items: Row 1 has 1 person who does 31 comparisons Row 2 has 2 people who do 15 comparisons each Row 3 has 4 people who do 7 comparisons each … 1

How many rows? How many times can we half a number before we get to 1? 2 1 (once if we start at 2) 4 2 1 (twice if we start at 4) 8 4 2 1 (three times if we start at 8) Mathematically the answer is the log2n log2 is just a symbol meaning how many times you can half a number! Each time we double the number it takes one more halving! 1

How much work? So to mergesort 32 items we will go back 5 levels of recursive calls - 5 rows of people. So if we go back 5 levels … When we mergesort 32 items: TOTAL Row 1: 1 person who does 31 comparisons 31 Row 2: 2 people who do 15 comparisons 30 Row 3: 4 people who do 7 comparisons 28 Row 3: 8 people who do 3 comparisons 24 Row 3: 16 people who do 1 comparison 16 … 1

Compare this to bubble sort When we bubble sort (efficiently) 32 items: TOTAL Pass 1 31 Pass 2 30 Pass 3 29 … Pass 31 1 Now consider the comparison when sorting a million items. NB log2 (1 million) = 20

1 Quicksort To quicksort an array: partition the array into two parts around a pivot quicksort those smaller arrays concatenate the two sorted arrays end to end Note it does option 2 - split takes time but combining them at the end is easy 1

The Partition Algorithm Partition is the key to this as it does all the work To Partition two sorted arrays Pick a pivot value Repeat until all entries processed Compare each value with the pivot Put those smaller to the left Put those larger to the right 1

Computational Thinking Lessons Algorithmic thinking Decomposition Evaluation Logical Thinking Generalisation Abstraction

Summary Focus on understanding algorithm first Sort algorithms can be introduced unplugged in a constructivist way Focus on understanding algorithm first Important it is directly linked to code too Also to do dry run exercises

Twitter: @TeachingLDNComp More support On our website to support this session: Activity sheets Story sheets Slides Details of more workshops/courses www.teachinglondoncomputing.org Twitter: @TeachingLDNComp

Twitter: @TeachingLDNComp @cs4fn Thank you! www.cs4fn.org www.teachinglondoncomputing.org Twitter: @TeachingLDNComp @cs4fn