SlideShare a Scribd company logo
1 of 7
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,  12,  11,  9,  3,  7 6,  2,  9,  11,  12,  9,  3,  7 6,  2,  9,  11,  9,  12,  3,  7 6,  2,  9,  11,  9,  3,  12,  7 6,  2,  9,  11,  9,  3,  7,  12 The 12 is greater than the 7 so they are exchanged. The 12 is greater than the 3 so they are exchanged. The twelve is   greater than the 9 so they are exchanged The 12 is larger than the 11 so they are exchanged. 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. Bubblesort compares the numbers in pairs from left to right exchanging when necessary.  Here the first number is compared to the second and as it is larger they are exchanged. The end of the list has been reached so this is the end of the first pass.  The twelve at the end of the list must be largest number in the list and so is now in the correct position.  We now start a new pass from left to right.
Bubble Sort Example 6,  2,  9,  11,  9,  3,  7,  12 2,  6,  9,  11,  9,  3,  7,  12 2,  6,  9,  9,  11,  3,  7,  12 2,  6,  9,  9,  3,  11,  7,  12 2,  6,  9,  9,  3,  7,  11,  12 6,  2,  9,  11,  9,  3,  7,  12 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. First Pass Second Pass
Bubble Sort Example 2,  6,  9,  9,  3,  7,  11,  12 2,  6,  9,  3,  9,  7,  11,  12 2,  6,  9,  3,  7,  9,  11,  12 6,  2,  9,  11,  9,  3,  7,  12 2,  6,  9,  9,  3,  7,  11,  12 Second Pass First Pass Third Pass This time the 11 and 12 are in position.  This pass therefore only requires 5 comparisons.
Bubble Sort Example 2,  6,  9,  3,  7,  9,  11,  12 2,  6,  3,  9,  7,  9,  11,  12 2,  6,  3,  7,  9,  9,  11,  12 6,  2,  9,  11,  9,  3,  7,  12 2,  6,  9,  9,  3,  7,  11,  12 Second Pass First Pass Third Pass Each pass requires fewer comparisons.  This time only 4 are needed. 2,  6,  9,  3,  7,  9,  11,  12 Fourth Pass
Bubble Sort Example 2,  6,  3,  7,  9,  9,  11,  12 2,  3,  6,  7,  9,  9,  11,  12 6,  2,  9,  11,  9,  3,  7,  12 2,  6,  9,  9,  3,  7,  11,  12 Second Pass First Pass Third Pass The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges. 2,  6,  9,  3,  7,  9,  11,  12 Fourth Pass 2,  6,  3,  7,  9,  9,  11,  12 Fifth Pass
Bubble Sort Example 2,  3,  6,  7,  9,  9,  11,  12 6,  2,  9,  11,  9,  3,  7,  12 2,  6,  9,  9,  3,  7,  11,  12 Second Pass First Pass Third Pass 2,  6,  9,  3,  7,  9,  11,  12 Fourth Pass 2,  6,  3,  7,  9,  9,  11,  12 Fifth Pass Sixth Pass 2,  3,  6,  7,  9,  9,  11,  12 This pass no exchanges are made so the algorithm knows 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.
Bubble Sort Example Quiz Time ,[object Object],Answer: The last number must be the largest. Answer: Each pass requires one fewer comparison than the last. Answer: When a pass with no exchanges occurs. ,[object Object],[object Object],[object Object],Answer: 9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45

More Related Content

Similar to Bubblesorting (12)

Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
NEGATIVE NUMBERS
NEGATIVE NUMBERSNEGATIVE NUMBERS
NEGATIVE NUMBERS
 
Introduction to fractions and concepts
Introduction to fractions and conceptsIntroduction to fractions and concepts
Introduction to fractions and concepts
 
GRADE 6 RATIO.pptx
GRADE 6 RATIO.pptxGRADE 6 RATIO.pptx
GRADE 6 RATIO.pptx
 
Unit 12: Probability
Unit 12: ProbabilityUnit 12: Probability
Unit 12: Probability
 
SSAT Answer Doc
SSAT Answer DocSSAT Answer Doc
SSAT Answer Doc
 
Fractions ppt
Fractions pptFractions ppt
Fractions ppt
 
Example Karen’S Math Review2
Example Karen’S Math Review2Example Karen’S Math Review2
Example Karen’S Math Review2
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Ratio
RatioRatio
Ratio
 
Math-Unit 7 Review
Math-Unit 7 ReviewMath-Unit 7 Review
Math-Unit 7 Review
 
Chapter2.2
Chapter2.2Chapter2.2
Chapter2.2
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Bubblesorting

  • 1. 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, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12 The 12 is greater than the 7 so they are exchanged. The 12 is greater than the 3 so they are exchanged. The twelve is greater than the 9 so they are exchanged The 12 is larger than the 11 so they are exchanged. 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. Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged. The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.
  • 2. Bubble Sort Example 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 11, 3, 7, 12 2, 6, 9, 9, 3, 11, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 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. First Pass Second Pass
  • 3. Bubble Sort Example 2, 6, 9, 9, 3, 7, 11, 12 2, 6, 9, 3, 9, 7, 11, 12 2, 6, 9, 3, 7, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.
  • 4. Bubble Sort Example 2, 6, 9, 3, 7, 9, 11, 12 2, 6, 3, 9, 7, 9, 11, 12 2, 6, 3, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass Each pass requires fewer comparisons. This time only 4 are needed. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass
  • 5. Bubble Sort Example 2, 6, 3, 7, 9, 9, 11, 12 2, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass
  • 6. Bubble Sort Example 2, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass Sixth Pass 2, 3, 6, 7, 9, 9, 11, 12 This pass no exchanges are made so the algorithm knows 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.
  • 7.