Bubble Sort Paul Curzon

Slides:



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

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Soda Constructor: Exploring the laws of Physics with Computational Thinking Paul Curzon Queen Mary University of London
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Make-a-face Prof Paul Curzon Queen Mary, University of London With support from Google, D of.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Hexahexaflexagon Automata Paul Curzon Queen Mary University of London With support from Google,
Bubble Sort.
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,
Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Change Blindness Presentation: Paul Curzon Change Blindness: Milan Verma & Peter McOwan, Queen.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.
Box Variables Prof Paul Curzon Queen Mary, University of London With support from Google, D of.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Week 9 - Monday CS 113.
Week 13: Searching and Sorting
Recitation 13 Searching and Sorting.
Introduction to Analysis of Algorithms
Lesson Objectives Aims Understand the following “standard algorithms”:
3.3 Fundamentals of data representation
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
Algorithm Analysis CSE 2011 Winter September 2018.
Punch Card Sorting: Binary Radix Sort
Mergesort: The power of divide and conquer
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.
Linear and Binary Search
The intelligent piece of paper: so what is an algorithm?
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Bubble Sort The basics of a popular sorting algorithm.
Describing algorithms in pseudo code
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Analysis of Bubble Sort and Loop Invariant
Bakuro: Binary Logical Thinking Puzzles
Quicksort analysis Bubble sort
The SwapPuzzle So what is an algorithm?
And now for something completely different . . .
Sodarace: Exploring Evolution with Computational Thinking
Queen Mary University of London
Sorting.
Standard Version of Starting Out with C++, 4th Edition
Search,Sort,Recursion.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
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.
Search,Sort,Recursion.
Data Structures & Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Sorting Dr. Yingwu Zhu.
CSCE 3110 Data Structures & Algorithm Analysis
10.3 Bubble Sort Chapter 10 - Sorting.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Bubble Sort Paul Curzon www.teachinglondoncomputing.org Queen Mary University of London CAS London With support from Google, BCS, Dept for Education the Mayor of London www.teachinglondoncomputing.org Twitter: @TeachingLDNComp

Aims Give you deeper understanding of core topics Sort algorithms including bubble sort 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

Compare adjacent entries Towards bubble sort Compare adjacent entries We can compare entries at a given position and swap them IF (array[position] > array [position+1]) THEN swap (array, position, position+1) 1

Towards bubble sort We can scan down the array doing that on adjacent pairs FOR position = 0 TO 3 { IF (array[position] > array [position+1]) THEN swap (array, position, position+1) } Is that enough to guarantee the array is sorted? 1

How many times do we do this We need to stop just before the end as the end entry has nothing to compare with So for an array of length 5, the last comparison is at position 4, to compare the 4th and 5th entries. However positions in arrays in many languages are numbered from 0 not 1 So that means it finishes comparing array[3] with array[4] www.teachinglondoncomputing.org

Towards bubble sort Multiple passes We need multiple passes i.e. to do that repeatedly FOR pass = 0 TO 3 { FOR position = 0 TO 3 IF (array[position] > array [position+1]) THEN swap (array, position, position+1) }

How many passes How many passes do we need to do to guarantee it is sorted? What is the worst situation we could be in? www.teachinglondoncomputing.org

How many passes On the first pass, the biggest value has ended up in the right place We took it with us, where ever it started. On the next pass the next biggest is in the right place…and so on When the second last one is in the right place there is no where else for the last one to go so it is right too. So if there are 10 entries in the array we will need 9 passes or more generally n entries need n-1 passes www.teachinglondoncomputing.org

A naive version of bubble sort bubblesort (array, n): FOR pass = 0 TO (n-2) { FOR position = 0 TO (n-2) IF (array[position] > array [position+1]) THEN swap (array, position, position+1) } Can we do better?

Can we do better? We have already seen that after the first pass that the biggest value is in the right place So why waste time comparing against something that we know isn't going to move? Similarly after 2 passes 2 entries are right (and so on) So on each pass there is one less thing to compare We need to stop the inner loop one place earlier on each pass www.teachinglondoncomputing.org

Can we do better? FOR position = 0 TO (n-2) … On pass 0 we make no change On pass 1 we subtract 1 from the stop point On pass 2 we subtract 2 from the stop point … We can do this just by subtracting pass FOR position = 0 TO (n-2) - pass … www.teachinglondoncomputing.org

A more efficient version of bubble sort bubblesort (array, n): FOR pass = 0 TO (n-2) { FOR position = 0 TO (n-2-pass) IF (array[position] > array [position+1]) THEN swap (array, position, position+1) } Can we do better?

Can we do better still? What happens if the array is already sorted? Over and over again we do comparisons, never changing anything Observation If we do a whole pass and nothing changes then it never will - the array is sorted Add a flag to detect when this happens and stop www.teachinglondoncomputing.org

A more efficient version of bubble sort bubblesort (array, n): changed := true pass := 0 WHILE (pass <= n-2) AND (changed = true) { changed := false FOR position = 0 TO (n-2-pass) { IF (array[position] > array [position+1]) THEN swap (array, position, position+1) } pass := pass + 1

Computational Thinking Lessons Algorithmic thinking Logical Thinking Generalisation Abstraction Decomposition Evaluation

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