Programming Techniques :: Sorting Algorithms

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Advertisements

CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Computer Programming Sorting and Sorting Algorithms 1.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Searching and Sorting Gary Wong.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
To know and use the Bubble Sort and Shuttle Sort 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.
ALGORITHMS.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Searching and Sorting Searching algorithms with simple arrays
CMPT 438 Algorithms.
Searching and Sorting Algorithms
GCSE/IGCSE-FM Functions
Growth of Functions & Algorithms
May 17th – Comparison Sorts
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Lecture 14 Searching and Sorting Richard Gesick.
Containers and Lists CIS 40 – Introduction to Programming in Python
Warmup What is an abstract class?
Simple Sorting Algorithms
Lesson Objectives Aims Understand the following “standard algorithms”:
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Teach A level Computing: Algorithms and Data Structures
10.3 Bubble Sort Chapter 10 - Sorting.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
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.
Linear and Binary Search
Algorithms Chapter 3 With Question/Answer Animations
Quicksort analysis Bubble sort
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Sort Techniques.
Lecture 11 Searching and Sorting Richard Gesick.
Data Structures Review Session
Standard Version of Starting Out with C++, 4th Edition
Search,Sort,Recursion.
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.
Data Structures Sorted Arrays
P1 Chapter 10 :: Trigonometric Identities & Equations
Basics of Recursion Programming with Recursion
Searching.
Search,Sort,Recursion.
Analysis of Algorithms
Unit 2: Computational Thinking, Algorithms & Programming
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Programming Techniques :: Records
Programming Techniques :: File Handling
Programming Techniques :: String Manipulation
Programming Techniques :: Flow Diagrams and Pseudocode
Data Representation :: Binary & Hexadecimal
Running & Testing :: IDEs
Programming Techniques :: Logic & Truth Tables
Programming Techniques :: Data Types and Variables
Networks :: Wireless Networks
Running & Testing Programs :: Translators
Programming Techniques :: Arithmetic & Boolean Operators
Programming Techniques :: Computational Thinking
Presentation transcript:

Programming Techniques :: Sorting Algorithms jamie@drfrostmaths.com www.drfrostmaths.com @DrFrostMaths Last modified: 30th June 2019

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. ?

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.

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. ?

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.

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)

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

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

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.

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. ? ? ? ? ? ?

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

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.

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.

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

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

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

Test Your Understanding Carry out a merge sort on the following numbers, showing your steps. 5 1 4 2 8 7 6 3 ? 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

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.

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×1023 2 =523776 comparisons, whereas merge sort would only require 1024× log 2 1024 =10240. That’s 51 times faster! 5 1 4 2 8 7 6 3 Split Merge

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).

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.

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! ? ? ? ?

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