Understanding Mergesort: The Power of Divide and Conquer in Sorting Algorithms
Explore the concept of mergesort, a divide-and-conquer algorithm that efficiently sorts arrays by splitting them into smaller arrays and merging them. Learn how mergesort works, the role of the merge algorithm, and the computational thinking behind it. Practical teaching methods and resources are also provided for a fun and engaging learning experience.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
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 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 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 1. split is easy but combining them at the end needs work 2. split takes time but combining them at the end is easy
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
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
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
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 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
More support On our website to support this session: Activity sheets Story sheets Slides Details of more workshops/courses www.teachinglondoncomputing.org Twitter: @TeachingLDNComp
Thank you! www.cs4fn.org www.teachinglondoncomputing.org Twitter: @TeachingLDNComp @cs4fn