Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Techniques :: Sorting Algorithms

Similar presentations


Presentation on theme: "Programming Techniques :: Sorting Algorithms"— Presentation transcript:

1 Programming Techniques :: Sorting Algorithms
Last modified: 30th June 2019

2 www.drfrostmaths.com ? Everything is completely free.
Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?

3 Motivation [“cat”, “bus”, “abacus”, “fish”]
We may want to put a list of values in ascending order, whether numerically or alphabetically. [“cat”, “bus”, “abacus”, “fish”]  [“abacus”, “bus”, “cat”, “fish”] Applications: It enables us to use a binary search if finding an item in the list. 1 There are many different well-known sorting algorithms. We will look at bubble sort, merge sort and insertion sort, but there are other algorithms such as quicksort. Some are more efficient than others! 2 We can find all the values within a range. 3 We can easily find the median and quartiles of numerical data. 4 Common for outputted data to be sorted, e.g. students in a class list are sorted by surname.

4 Bubble Sort We look at each pair of values in turn, and swap them if they’re in the incorrect order: Click to Animate 31 19 55 42 2 112 67 This is known as ‘one pass’ of the algorithm. Clearly we’re not done, but we definitely know one value will be in the correct place. Which? The last value (112), because it’s guaranteed to be moved right on each comparison. ? We can then do a ‘second pass’, and can exclude the last value, given we know it’s now in the correct position: Click to Animate 19 31 42 55 2 67 112 After the second pass, we know that the 67 is now in the right place. We keep doing passes, ignoring one extra item at the end each time. We can stop if there are no swaps on a pass. ?

5 Analysis of Bubble Sort
On each pass, look at each pair of terms left-to-right, swapping if in the wrong order. Keep doing further passes, ignoring one extra value at the end each time. We are done if there are no swaps on a pass. Exam Tip: Bubble Sort should not be confused with a ‘Buble Sort’, which should be avoided at all costs. Pros Cons ? ? Simple to implement. Memory efficient, as we can use just the list without any extra variables required. Efficient way to check if list is in order, as if list of size 𝑛, only 𝑛−1 comparisons needed Extremely inefficient. We need 𝑛−1 + 𝑛−2 +…+3+2+1= 𝑛 𝑛−1 2 comparisons. Since this is a quadratic expression, we say bubble sort takes quadratic time. Fun Fact: The algorithm is called ‘bubble sort’ because the greatest value ‘bubbles up’ to the end of the list on each pass.

6 Pseudocode A is our list. procedure bubbleSort(A) n = length(A)
repeat swapped = false for i = 1 to n-1 do if A[i-1] > A[i] then swap( A[i-1], A[i] ) swapped = true end if end for n = n - 1 until not swapped end procedure This repeat allows us to do multiple passes. This considers the pairs left-to-right. We swap the items if they’re in the wrong order, and remember we did a swap. By decrementing 𝑛 each time, we can ignore an extra item at the end of the list on each pass. We’re done if no swaps occurred on a pass. Notice we required no extra variables (except the flag to remember if we swapped)

7 What should we write in exams?
How to write in an exam: Label your passes (“Pass 1”, …) For the first pass, show each individual swap. Underline the items if a swap occurred. For each subsequent pass, just show the order of the terms at the end of that pass. ? Pass 1: bait crime crime fright fright victory nymph victory loose victory bait crime fright nymph loose victory Pass 2: bait crime fright loose nymph victory

8 Test Your Understanding
Perform a bubble sort on the following list, showing the stages. ? Pass 1: 2 5 4 5 1 5 5 6 Pass 2: Pass 3:

9 Insertion Sort #1: We consider each position in the list from left-to-right. We start with the first… #2: We consider all items to the left of it, and insert it into the correct place. There’s nothing to the left of it, so nothing to do yet! #3: Now consider the second position. We consider all terms to the left, and insert it into the correct place, shifting other values up where necessary. 6 5 3 1 8 7 #4: Now the third value… #5: And the fourth… #6: And the fifth. The 8 is in the right place so need not be inserted. #7: And this continues until we get to the end of the list.

10 What to write in an exam How to do an insert sort in an exam: Write the current list at the start of each step. After each line indicate what action you are about to take. Underline the item you’re currently considering (starting from the second). These should go diagonally. Carry out an insertion sort on the following words, showing your steps. Ashwin Shirav Paula Tina Shota Matthew Rishi ? Ashwin Shirav Paula Tina Shota Matthew Rishi No insert Ashwin Shirav Paula Tina Shota Matthew Rishi Insert Paula between Ashwin & Shirav Ashwin Paula Shirav Tina Shota Matthew Rishi No insert Ashwin Paula Shirav Tina Shota Matthew Rishi Insert Shota between Shirav & Tina Ashwin Paula Shirav Shota Tina Matthew Rishi Insert Matt between Ashwin & Paula Ashwin Matthew Paula Shirav Shota Tina Rishi Insert Rishi between Paula & Shirav Ashwin Matthew Paula Rishi Shirav Shota Tina Completed. ? ? ? ? ? ?

11 Test Your Understanding
Carry out an insertion sort on the following numbers, showing your steps. ? Insert before 5 Insert before 3 Insert between 1 & 3 No insert Sort completed

12 Pseudocode i represents the position of the item currently being considered. We start from the second item at position 1 (rather than the first item at position 0), because the first item doesn’t have anything to the left of it. i ← 1 while i < length(A) j ← i while j > 0 and A[j-1] > A[j] swap A[j] and A[j-1] j ← j - 1 end while i ← i + 1 We consider each item up to the end of the list. This loop does the insert. It works backwards starting from the item we’re currently considering. It swaps the item with the one before before it, and continues until it’s been inserted in the correct place. Consider the next item.

13 Analysis of Insertion Sort
Pros Cons ? ? Again, memory efficient, as we can use just the list without any extra variables required. Again, also quick at checking if list is already sorted. Quickly adds items to an already sorted list. Like bubble sort, requires 𝑛 𝑛−1 2 comparisons in worst case (if list is in perfect reverse order). So again, quadratic time.

14 Merge Sort Merge sort is the most complicated of these three sorting algorithms, so pay attention! However, it is in general significantly more efficient than the other two, in terms of requiring less comparisons of values. 7 3 1 6 8 2 5 Keep splitting the list in half until each list has just one item. (If an odd number, the first half gets the extra item) Split 7 3 1 6 8 2 5 Split 7 3 1 6 8 2 5 Split Then merge back the lists, sorting each time. We’ll see how this step is done in a sec… 7 3 1 6 8 2 5 Merge 3 7 1 6 2 8 5 Merge Eventually we’ll have one list again, which will be sorted. 1 3 6 7 2 5 8 Merge 1 2 3 5 6 7 8

15 How do we do the merge step?
On the previous slide, each merge step was able to combine two sorted lists into one sorted list, using very few comparisons (just one comparison per item). Instructions: Start with a marker at the beginning of each list. Compare the two elements at the markers. The lowest value gets put in the new list, and the marker at that item used moves up one. Then repeat! Click to Animate 19 31 55 112 2 4 42 67 New merged list

16 Further Example Note that, in an exam, you should show a merge sort in exactly the way that has been shown. Carry out a merge sort on the following numbers, showing your steps. 5 3 1 2 8 6 Split 5 3 1 2 8 6 Split 5 3 1 2 8 6 Split 5 3 1 2 8 6 Merge 3 5 1 2 8 6 Merge 1 3 5 2 6 8 Merge 1 2 3 5 6 8

17 Test Your Understanding
Carry out a merge sort on the following numbers, showing your steps. ? 5 1 4 2 8 7 6 3 Split 5 1 4 2 8 7 6 3 Split 5 1 4 2 8 7 6 3 Split 5 1 4 2 8 7 6 3 Merge 1 5 2 4 7 8 3 6 Merge 1 2 4 5 3 6 7 8 Merge 1 2 3 4 5 6 7 8

18 Analysis of Merge Sort ? ? Pros Cons
For all but short lists, it is much more efficient than insertion and bubble sorts (full analysis on next slide). It has a predictable running time (we always do the same number of comparisons whether it’s already sorted or in reverse order) Slow for small lists (but this is hardly a con!). And if list is already sorted, it still has to do full process of splitting and merging. Requires more memory as we have to construct various new lists.

19 Advanced :: Time Analysis of Merge Sort
(Not in GCSE syllabus) The process of splitting requires no comparisons. We saw that each merge involves exactly 1 comparison per value. So if the list is length 𝑛, each full merge line requires 𝒏 comparisons. There are 3 merge lines. We could have obtained this number by determining how many times we need to halve 8 until we get to 1. Since 2 3 =8, this was 3 times. Recall, from the topic of Search Algorithms, that we can use the log function to obtain this number; i.e. there will be log 2 𝑛 lines, and in our example, log 2 8 =3. Thus if we have log 2 𝑛 lines each requiring 𝑛 comparisons, that’s 𝒏 𝐥𝐨𝐠 𝟐 𝒏 comparisons in total. Suppose we have 1024 values in a list. Then bubble sort would require 1024× = comparisons, whereas merge sort would only require 1024× log = That’s 51 times faster! 5 1 4 2 8 7 6 3 Split Merge

20 Sorting Functions in JavaScript
JavaScript has a method sort() for arrays. Note that the values in the array will be changed, rather than returning a new array: var nums = [2, 4, 1, 6, 5] nums.sort(); console.log(nums); [1, 2, 4, 5, 6] The JavaScript language specification doesn’t prescribe a particular sorting algorithm to use for sort. But Google Chrome uses insertion sort for arrays less than 10 values in length, and quick sort (a sorting algorithm we didn’t cover) for longer arrays (recall that insertion sort is quick for small lists).

21 Custom Ordering in JavaScript
This is all well and good if the values have some natural ordering (numerical or alphabetical), but what if the ordering wasn’t obvious, e.g. we had an array of JSONs: Suppose we wanted to sort by age… var students = [{name: “Ashwin Hose”, age: 7}, {name: “Arthur Weasley”, age: 45}, {name: “Harry Potter”, age: 12} ] The solution is to have a custom comparison function which compares two items at a time. students.sort(function(a,b) { if(a.age < b.age)return -1; else if(a.age > b.age)return 1; else return 0; }); This is known as an anonymous function because it doesn’t have a name. Anonymous functions are useful when we want to specify a function for a single use only (i.e. it doesn’t need to be referred to elsewhere). sort expects a function which takes two items a and b we wish to compare. The comparison function expects us to return: A negative number if 𝑎<𝑏. We’ve used −1 above but any negative number will do. A positive number if 𝑎>𝑏 0 if they are equal. We could write these 3 lines more concisely using: return b.age – a.age; Think why this works.

22 Review ? ? ? ? Give an advantage of insertion sort over merge sort.
Either: (a) faster on small lists or (b) requires less memory, as sort occurs on the original list without need for extra variables. In the word case, how many comparisons does the bubble sort require for 7 values? 7 7−1 2 =21 comparisons. What is the main advantage of merge sort? In general it requires far fewer operations/comparisons. For what kind of lists can we apply a sorting algorithm? Any list where the values have some natural ordering. This might be alphabetical order for characters/strings, and numerical order for integers/reals. Otherwise, we would have to specify a custom comparison function. Make sure you know how to carry out each of the sorting algorithms and what you would write down in an exam! ? ? ? ?

23 Coding Mini-Tasks Return to the DrFrostMaths site to complete the various mini-coding tasks on sorting algorithms.


Download ppt "Programming Techniques :: Sorting Algorithms"

Similar presentations


Ads by Google