Queen Mary University of London

Slides:



Advertisements
Similar presentations
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Advertisements

Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
CPS120: Introduction to Computer Science Searching and Sorting.
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.
Selection Sort Given n numbers to sort: Repeat the following n-1 times:  Mark the first unsorted number  Find the smallest unsorted.
Standard Grade Computing SYSTEM SOFTWARE CHAPTER 19.
Soda Constructor: Exploring the laws of Physics with Computational Thinking Paul Curzon Queen Mary University of London
Recursion, Complexity, and Sorting By Andrew Zeng.
1 Data Structures and Algorithms Sorting I Gal A. Kaminka Computer Science Department.
The Mind of the Machine: Artificial Intelligence? Paul Curzon Queen Mary, University of London Created by Peter McOwan and Paul Curzon of Queen Mary,
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
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.
To know and use the Bubble Sort and Shuttle Sort Algorithms.
Hexahexaflexagon Automata Paul Curzon Queen Mary University of London With support from Google,
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
3 – SIMPLE SORTING ALGORITHMS
The Imp Computer Prof Paul Curzon Queen Mary, University of London With support from Google,
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Computational Thinking Activities The Magic of Computer Science
CPS120: Introduction to Computer Science Sorting.
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.
Lists and Sorting Algorithms
CPS120: Introduction to Computer Science Sorting.
Searching and Sorting Searching algorithms with simple arrays
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Searching and Sorting Algorithms
May 17th – Comparison Sorts
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Sorting Why? Displaying in order Faster Searching Categories Internal
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
3.3 Fundamentals of data representation
Punch Card Sorting: Binary Radix Sort
Strings: Tries, Suffix Trees
Bubble Sort Paul Curzon
Mergesort: The power of divide and conquer
The intelligent piece of paper: so what is an algorithm?
Bubble, Selection & Insertion sort
Dynamic Programming.
List Algorithms Taken from notes by Dr. Neil Moore
Bakuro: Binary Logical Thinking Puzzles
Quicksort analysis Bubble sort
The SwapPuzzle So what is an algorithm?
Roisin Paddy Mary Eamon Cora ….
Sodarace: Exploring Evolution with Computational Thinking
An Introduction to VEX IQ Programming with Modkit
Broom Dancing.
Bubble Sort Key Revision Points.
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.
Chapter 10: Algorithm TECH Prof. Jeff Cheng.
Searching.
Sorting Example Bubble Sort
Roisin Paddy Mary Eamon Cora ….
CS 1114: Sorting and selection (part two)
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Stage 5: Getting Loopy.
Insertion Sort and Shell Sort
Sorting Sorting is a fundamental problem in computer science.
CS 165: Project in Algorithms and Data Structures Michael T. Goodrich
Programming Techniques :: Sorting Algorithms
Presentation transcript:

Queen Mary University of London The Bubble Sort Dance Paul Curzon Queen Mary University of London CAS London www.cs4fn.org www.teachinglondoncomputing.org Twitter: @cs4fn

Aims We are going to explore the way computers sort data We will also see how dances and programs have a lot in common

Code = Dance Choreographing a dance is very similar to writing code. You have to come up with the right steps in the right order. Once you have created a dance anyone can follow the instructions without having to invent the dance anew. The dancers follow the steps of the dance just like a computer follows the steps of a program Programmers choreograph the way data dances round a computer memory.

Sort Algorithms A sort algorithm takes a series of things and puts it into 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 putting your music collection in alphabetical order so you can find tracks).

The Sorting Dance We are going to choreograph a sort algorithm as though it were a dance. We need 5 volunteers to be our dancers They each get a different number on their back They start by standing in a line.

The sorting dance (1) The following is the basic steps that make up the dance Pass hat to first dancer Dancer with hat and next dancer step forward if dancer with hat > next dancer Dancer with hat swaps places with next dancer else Dancer with hat passes it to next dancer The pair step back in to the line

The sorting dance (2) We need to do that for each pair, down the line: Pass hat to first dancer Repeat 4 times { Dancer with hat and next dancer step forward if dancer with hat > next dancer Dancer with hat swaps places with next dancer else Dancer with hat passes them to next dancer The pair step back in to the line }

The sorting dance (3) Doing one pass isn’t enough we need to do it over and over again Repeat 4 times { Pass hat to first dancer Dancer with hat and next dancer step forward if dancer with hat > next dancer Dancer with hat swaps places with next dancer else Dancer with hat passes them to next dancer The pair step back in to the line }

How many passes How many passes do we need to do to guarantee the dancers are in sorted order? We have to be sure it works even if everyone starts in the worst possible positions.

How many passes On the first pass, the biggest value has ended up in the right place It went with the hat 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 dancer to go so it is right too. So if there are 5 dancers we will need 4 passes or more generally n dancers needs n-1 passes

Can we do better? This is quite a simple version of bubble sort It is possible to come up with an improved version that involves less steps so the dance is much shorter Can you work out how?

Doing better still What happens if the array is already sorted? Over and over again we do comparisons, lots of wasted dance steps never changing anything

Doing better still Observation If we do a whole pass and nothing changes then it never will - the array is sorted So we can shorten the dance further by keeping track of whether we swap anything on a pass and stopping at the end of any pass where none swapped. Now we have a dance that will be a different length depending on where the numbers are at the start.

Summary We have seen one simple way to sort data. We have also seen how writing programs is similar to choreographing a dance. Both need lots of creativity.

More on the fun side of computer science from: www.cs4fn.org www.teachinglondoncomputing.org Twitter: @cs4fn With support from Google, BCS, Dept for Education, the Mayor of London and the BBC