3.3 Fundamentals of data representation

Slides:



Advertisements
Similar presentations
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
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,
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
Searching and Sorting Arrays
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.
CSC220 Data Structure Winter
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
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.
Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,
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.
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.
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.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Bubble Sort Example
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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])
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
In the first pass, the first two numbers are compared. The shuttle sort compares the numbers in pairs from left to right exchanging when necessary.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CMPT 120 Topic: Sorting Algorithms – Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n -> O(n) i.e., has.
Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.
Bubble Sort!. What is a bubble sort?!?!?!?!?!?!?!? In a bubble sorting algorithm, the elements of the list "gradually 'bubble' (or rise) to their proper.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
3.3 Fundamentals of data representation
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS212: Data Structures and Algorithms
Searching and Sorting Algorithms
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
Algorithm Efficiency and Sorting
Bubble Sort The basics of a popular sorting algorithm.
Bubble, Selection & Insertion sort
Analysis of Bubble Sort and Loop Invariant
Shuttle Sort Example 1st pass Comparisons: 1
And now for something completely different . . .
“Human Sorting” It’s a “Problem Solving” game:
Sorting Algorithms Ellysa N. Kosinaya.
Searching and Sorting Arrays
Queen Mary University of London
Standard Version of Starting Out with C++, 4th Edition
IT 4043 Data Structures and Algorithms
Lecture 16 Bubble Sort Merge Sort.
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Principles of Computing – UFCFA3-30-1
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.
Shell Sort and Merge Sort
Visit for More Learning Resources
Searching and Sorting Arrays
Sorting Example Bubble Sort
CS 101 – Oct. 21 Sorting Much-studied problem in CS – many ways to do it Given a list of data, need to arrange it “in order” Some methods do better based.
Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort.
Searching and Sorting Arrays
Introduction to Sorting Algorithms
Shuttle Sort Example 1st pass Comparisons: 1
Principles of Computing – UFCFA3-30-1
“Human Sorting” It’s a “Problem Solving” game:
CHAPTER 9 SORTING & SEARCHING.
Visit for more Learning Resources
Presentation transcript:

3.3 Fundamentals of data representation 3.1.4 Sorting algorithms 1 Lesson

What is this all about? Watch this video made by Sapientia University in Romania from 0:50 minutes to 4:00 minutes: youtube.com/watch?v=lyZQPjUT5B4 Look carefully at what is happening – the numbers on the dancers will help. When the video has finished you will have two minutes to discuss what was happening with your partner.

What did you notice? The dancers were gradually being arranged in order from 1 to 9. Dancer 9 was put in place first, followed by 8, 7, etc. – down to 1. It took quite a long time! They were following an algorithm called a ‘Bubble Sort’.

Understand and explain how the bubble sort algorithm works. Objective Understand and explain how the bubble sort algorithm works.

What is a bubble sort? A bubble sort algorithm is a simple method used to arrange a data set in order. An array of data is traversed continually and adjacent pairs of elements are swapped, if required, until the array is placed in the desired order. A bubble sort is likely to require multiple passes through the array and this makes this method inefficient.

Creating the algorithm Here is another short video clip. Watch carefully from 0:48 minutes to 3:08 minutes: youtube.com/watch?v=NiyEqLZmngY&nohtml5=False When it has finished you will have five minutes to come up with your own version of the bubble sort algorithm.

Bubble sort algorithm Did you get something like this? Step 1: Compare the first two numbers. If the smaller number is on the right, swap these two numbers. Write the remainder of the list. Step 2: Move to the next number in the list and compare these two numbers. If the smaller number is on the right, swap these two numbers. Write the remainder of the list. Step 3: Repeat Step 2 until the last two numbers have been compared. This completes the FIRST PASS. Step 4: SECOND PASS. Repeat steps 1, 2 and 3. No need to compare the last two places. Step 5: THIRD PASS. Repeat step 1, 2 and 3. No need to compare the last three places. STOP: When a complete pass produces no swaps.

Bubble sort example 1st pass 2nd pass 3rd pass 4th pass 7 2 9 5 4 2 7 5 4 9 2 5 4 7 9 2 4 5 7 9 2 7 9 5 4 2 7 5 4 9 2 5 4 7 9 2 4 5 7 9 2 7 9 5 4 2 5 7 4 9 2 4 5 7 9 2 7 5 9 4 2 5 4 7 9 10 total comparisons 6 total swaps 2 7 5 4 9 How many comparisons and swaps? How many comparisons and swaps? How many comparisons and swaps? How many comparisons and swaps? 4 comparisons 3 swaps 3 comparisons 2 swaps 2 comparisons 1 swaps 1 comparisons 0 swaps

Over to you… Complete the worksheet.

Bubble sort quiz After the first pass, one number will definitely be in the correct position. Which number is that and in which position? It will be the largest number and it will be in the last position. After each pass through the data, are there more or less comparisons made? Each pass through the data requires one less comparison that the pass before.

Bubble sort quiz Eventually the algorithm stops. How does it know when to stop? There will be no swaps. Write down a list of five numbers which will need just one pass through the list and no swaps. e.g. 1 2 3 4 5

Bubble sort quiz Write down a set of five numbers that will need the maximum number of comparisons and swaps before it is sorted. How many comparisons are needed in total. 5 4 3 2 1 Comparisons Swaps Pass Totals 10 What is the maximum number of comparisons for a list of 10 numbers? 45