Bubble Sort Example 6 9 2 12 11 9 3 7 9 6 2 12 11 9 3 7 6 2 9 11 9 3 12 7 6 2 9 11 9 3 7 12 6.

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

Consider an array of n values to be sorted into ascending order. Sorting.
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 3, 9, 3, 9, 7, 3, 9, 7, 6, First we use the number in the centre of the list as a ‘pivot’. We then.
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,
Type Title Here for Tic-Tac-Toe Type names of students in group here.
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)
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.
Searching and Sorting Arrays
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.
By: Vishal Kumar Arora AP,CSE Department, Shaheed Bhagat Singh State Technical Campus, Ferozepur. Different types of Sorting Techniques used in Data Structures.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Searching and Sorting Gary Wong.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
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,
Decision Maths 1 Sorting Algorithm Shuttle Sort A V Ali : 1.Compare items 1 and 2; swap them if necessary 2.Compare 2 and 3; swap.
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.
3.3 Complexity of Algorithms
Selection Sort Comparison Data Movement Sorted.
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.
ALGORITHMS.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Bubble Sort Notes David Beard CIS220. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
Example 1 Writing Powers Write the product as a power and describe it in words. a. 44= to the second power, or 4 squared 9 to the third power,
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.
Document that explains the chosen concept to the animator 1.
Bubble Sort!. What is a bubble sort?!?!?!?!?!?!?!? In a bubble sorting algorithm, the elements of the list "gradually 'bubble' (or rise) to their proper.
Sort Algorithm.
The Bubble Sort Mr. Dave Clausen La Cañada High School
CS212: Data Structures and Algorithms
Searching and Sorting Algorithms
Bin Packing First fit algorithm
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Sorting Algorithms.
3.3 Fundamentals of data representation
Insertion Sort Sorted Unsorted
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
Algorithm Efficiency and Sorting
Selection Sort Sorted Unsorted Swap
Shuttle Sort Example 1st pass Comparisons: 1
Bin packing – First fit algorithm
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
IT 4043 Data Structures and Algorithms
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.
PRIME FACTORIZATION USING FACTOR TREES!.
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.
Searching and Sorting Arrays
Sorting Example Bubble Sort
Quiz Name Here Click to start.
Quiz Name Here Click to start.
Sorting Damian Gordon.
Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort.
ANGLE PAIRS.
Bin Packing First fit algorithm
Introduction to Sorting Algorithms
Shuttle Sort Example 1st pass Comparisons: 1
CHAPTER 9 SORTING & SEARCHING.
Presentation transcript:

Bubble Sort Example 6 9 2 12 11 9 3 7 9 6 2 12 11 9 3 7 6 2 9 11 9 3 12 7 6 2 9 11 9 3 7 12 6 2 9 12 11 9 3 7 6 2 9 11 12 9 3 7 6 2 9 11 9 12 3 7 The bubble sort compares the numbers in pairs from left to right exchanging when necessary. The end of the list has been reached so this is the end of the first pass. The 12 at the end of the list must be the largest number in the list and so is now in the correct position. We now start a new pass from left to right. In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list. Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged. Here the first number is compared to the second and as it is larger they are exchanged. The 12 is greater than the 3 so they are exchanged. The 12 is larger than the 11 so they are exchanged. The 12 is greater than the 9 so they are exchanged The 12 is greater than the 7 so they are exchanged. Comparisons: 1 6 7 4 5 2 3 Swaps: 5 4 6 2 1 3

Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 Second Pass 2 6 9 9 3 11 7 12 2 6 9 9 11 3 7 12 2 6 9 9 3 7 11 12 2 6 9 11 9 3 7 12 6 2 9 11 9 3 7 12 The end of the list has been reached so this is the end of the first pass. The 12 at the end of the list must be the largest number in the list and so is now in the correct position. We now start a new pass from left to right. Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons. Comparisons: 10 9 11 13 7 12 8 Swaps: 10 9 8 7 6

Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 Second Pass 2 6 9 9 3 7 11 12 Third Pass 2 6 9 9 3 7 11 12 2 6 9 3 7 9 11 12 2 6 9 3 9 7 11 12 The 11 and 12 are in the correct positions. The next pass therefore only requires 5 comparisons. Comparisons: 16 17 15 14 13 18 Swaps: 12 11 10

Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 Second Pass 2 6 9 9 3 7 11 12 Third Pass 2 6 9 3 7 9 11 12 Fourth Pass 2 6 3 9 7 9 11 12 2 6 3 7 9 9 11 12 2 6 9 3 7 9 11 12 Each pass requires fewer comparisons. This time only 4 are needed. Comparisons: 20 21 22 18 19 Swaps: 14 12 13

Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 Second Pass 2 6 9 9 3 7 11 12 Third Pass 2 6 9 3 7 9 11 12 Fourth Pass 2 6 3 7 9 9 11 12 Fifth Pass 2 3 6 7 9 9 11 12 2 6 3 7 9 9 11 12 The list is now sorted but the algorithm does not “know” this until it completes a pass with no exchanges. Comparisons: 24 25 23 22 Swaps: 15 14

Bubble Sort Example First Pass 6 2 9 11 9 3 7 12 Second Pass 2 6 9 9 3 7 11 12 Third Pass 2 6 9 3 7 9 11 12 Fourth Pass In this pass no exchanges have been made so the algorithm “knows” that the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work. 2 6 3 7 9 9 11 12 Fifth Pass 2 3 6 7 9 9 11 12 Sixth Pass 2 3 6 7 9 9 11 12 Comparisons: 27 26 25 Swaps: 15

Bubble Sort Example Quiz Time Which number is definitely in its correct position at the end of the first pass? Answer: The last number must be the largest. How does the number of comparisons required change as the pass number increases? Answer: Each pass requires one fewer comparison than the last. How does the algorithm “know” when the list is sorted? Answer: When a pass with no exchanges occurs. What is the maximum number of comparisons required for a list of 10 numbers? Answer: 9 comparisons in the first pass, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45