Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mergesort: The power of divide and conquer

Similar presentations


Presentation on theme: "Mergesort: The power of divide and conquer"— Presentation transcript:

1 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

2 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:

3 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

4 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

5 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

6 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

7 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

8 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

9 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) (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

10 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 Row 2: 2 people who do 15 comparisons Row 3: 4 people who do 7 comparisons Row 3: 8 people who do 3 comparisons Row 3: 16 people who do 1 comparison 1

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

12 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

13 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

14 Computational Thinking Lessons
Algorithmic thinking Decomposition Evaluation Logical Thinking Generalisation Abstraction

15 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

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

17 Twitter: @TeachingLDNComp @cs4fn
Thank you! @cs4fn


Download ppt "Mergesort: The power of divide and conquer"

Similar presentations


Ads by Google