SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Why the name ‘Bubble Sort’
•The bubble sort gets its name because elem
ents tend to move up into the correct order
like bubbles rising to the surface.
•or
•The elements of the list "gradually 'bubble'
(or rise) to their proper location in the array
, like bubbles rising in a glass of soda"
Data Structure - Bubble Sort
Algorithm
•Bubble sort is a simple sorting algorithm.
•This sorting algorithm is comparison-based
algorithm in which each pair of adjacent
elements is compared and the elements are
swapped if they are not in order.
How does a bubble sort algorithm works
?
•Bubble sort algorithms cycle through a list, analyzing p
airs of elements from left to right, or beginning to end.
•If the left most element in the pair is less than the
rightmost element, the pair will remain in that order.
•If the rightmost element is less than the leftmost elemen
t, then the two elements will be switched.
•This cycle repeats from beginning to end until a pass in
which no switch occurs.
AlgorithmBubble(a,n)
1.Repeat step2 and 3 for k=1 to n-1
2.Set i=1
3.Repeat while i<=n-k
(a) If a[i]>a[i+1], then
interchange a[i] and a[i+1]
(b) set i=i+1
4. Exit
Example A: 5, 12, 3, 9, 16
Pass 1
● 5, 12, 3, 9, 16
○ The list stays the same because 5 is less than 12.
● 5, 12, 3, 9, 16
○ 3 and 12 are switched because 3 is less than 12
● 5, 3, 12, 9, 16
○ 9 and 12 are switched since 9 is less than 12
● 5, 3, 9, 12, 16
○ 12 and 16 do not switch because 12 is less than 16
Example A: 5, 12, 3, 9, 16
Pass 2
● 5, 3, 9, 12, 16
○ 3 is less than 5, so they switch
● 3, 5, 9, 12, 16
○ 5 is less than 9 so they remain in the same places
● 3, 5, 9, 12, 16
○ 12 is greater than 9 so they do not switch places
● 3, 5, 9, 12, 16
○ 12 and 16 are in numerical order so they don’t switch.
Example A: 5, 12, 3, 9, 16
Pass 3
● 3, 5, 9, 12, 16
○ 3 is less than 5, so they do not switch
● 3, 5, 9, 12, 16
○ 5 is less than 9 so they remain in the same places
● 3, 5, 9, 12, 16
○ 12 is greater than 9 so they do not switch places
● 3, 5, 9, 12, 16
○ 12 and 16 are in numerical order so they don't switch
Rabbits And Turtles
•Higher value elements, or elements that occur
at the end of the sequence, take few passes and
switches to get to the right spots. These are calle
d “rabbits”
• Lower value elements move very slowly to thei
r
correct places. These are called “turtles”
2 4 1 8 3
2 4 8 3 1
Example 2: 25,17,31,13,2
Lets check
Memory Efficiency and Data Structures
● The bubble sort is a very memory-efficient because al
l of the ordering occurs within the array or list itself .
No new memory is allocated.
● No new data structures are necessary, for the same
reason.
Advantages of the Bubble Sort
● The bubble sort requires very little memory other than that whi
ch
the array or list .
● The bubble sort is comprised of relatively few lines of code.
● With a best-case running time of O(n), the bubble sort is good fo
r testing whether a list is sorted or not.
● Other sorting methods often cycle through their whole sorting
sequence, which often have running times of O(n^2) or O(n log n
) for this task.
● The same applies for data sets that have only a few items that ne
Disadvantages of the Bubble Sort
● The main disadvantage of the bubble sort method is th
e time it requires. With a running time of O(n^2), it is
highly inefficient for large data sets.
● Additionally, the presence of turtles
can severely slow the sort.
Bubble sort

Weitere ähnliche Inhalte

Was ist angesagt?

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 

Was ist angesagt? (20)

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Binary search
Binary searchBinary search
Binary search
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Red black tree
Red black treeRed black tree
Red black tree
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 

Ähnlich wie Bubble sort

Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
ArjayBalberan1
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
vital vital
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-le
Sumedha
 

Ähnlich wie Bubble sort (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algos
Sorting algosSorting algos
Sorting algos
 
Sorting
SortingSorting
Sorting
 
one main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersone main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to others
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
sorting_part1.ppt
sorting_part1.pptsorting_part1.ppt
sorting_part1.ppt
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Sorting
SortingSorting
Sorting
 
BUBBLESORT
BUBBLESORT BUBBLESORT
BUBBLESORT
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Sorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptxSorting Data structure And Algorithm.pptx
Sorting Data structure And Algorithm.pptx
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
 
Sorting
SortingSorting
Sorting
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-le
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Bubble sort

  • 1.
  • 2. Why the name ‘Bubble Sort’ •The bubble sort gets its name because elem ents tend to move up into the correct order like bubbles rising to the surface. •or •The elements of the list "gradually 'bubble' (or rise) to their proper location in the array , like bubbles rising in a glass of soda"
  • 3. Data Structure - Bubble Sort Algorithm •Bubble sort is a simple sorting algorithm. •This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order.
  • 4. How does a bubble sort algorithm works ? •Bubble sort algorithms cycle through a list, analyzing p airs of elements from left to right, or beginning to end. •If the left most element in the pair is less than the rightmost element, the pair will remain in that order. •If the rightmost element is less than the leftmost elemen t, then the two elements will be switched. •This cycle repeats from beginning to end until a pass in which no switch occurs.
  • 5. AlgorithmBubble(a,n) 1.Repeat step2 and 3 for k=1 to n-1 2.Set i=1 3.Repeat while i<=n-k (a) If a[i]>a[i+1], then interchange a[i] and a[i+1] (b) set i=i+1 4. Exit
  • 6. Example A: 5, 12, 3, 9, 16 Pass 1 ● 5, 12, 3, 9, 16 ○ The list stays the same because 5 is less than 12. ● 5, 12, 3, 9, 16 ○ 3 and 12 are switched because 3 is less than 12 ● 5, 3, 12, 9, 16 ○ 9 and 12 are switched since 9 is less than 12 ● 5, 3, 9, 12, 16 ○ 12 and 16 do not switch because 12 is less than 16
  • 7. Example A: 5, 12, 3, 9, 16 Pass 2 ● 5, 3, 9, 12, 16 ○ 3 is less than 5, so they switch ● 3, 5, 9, 12, 16 ○ 5 is less than 9 so they remain in the same places ● 3, 5, 9, 12, 16 ○ 12 is greater than 9 so they do not switch places ● 3, 5, 9, 12, 16 ○ 12 and 16 are in numerical order so they don’t switch.
  • 8. Example A: 5, 12, 3, 9, 16 Pass 3 ● 3, 5, 9, 12, 16 ○ 3 is less than 5, so they do not switch ● 3, 5, 9, 12, 16 ○ 5 is less than 9 so they remain in the same places ● 3, 5, 9, 12, 16 ○ 12 is greater than 9 so they do not switch places ● 3, 5, 9, 12, 16 ○ 12 and 16 are in numerical order so they don't switch
  • 9. Rabbits And Turtles •Higher value elements, or elements that occur at the end of the sequence, take few passes and switches to get to the right spots. These are calle d “rabbits” • Lower value elements move very slowly to thei r correct places. These are called “turtles” 2 4 1 8 3 2 4 8 3 1
  • 11. Memory Efficiency and Data Structures ● The bubble sort is a very memory-efficient because al l of the ordering occurs within the array or list itself . No new memory is allocated. ● No new data structures are necessary, for the same reason.
  • 12. Advantages of the Bubble Sort ● The bubble sort requires very little memory other than that whi ch the array or list . ● The bubble sort is comprised of relatively few lines of code. ● With a best-case running time of O(n), the bubble sort is good fo r testing whether a list is sorted or not. ● Other sorting methods often cycle through their whole sorting sequence, which often have running times of O(n^2) or O(n log n ) for this task. ● The same applies for data sets that have only a few items that ne
  • 13. Disadvantages of the Bubble Sort ● The main disadvantage of the bubble sort method is th e time it requires. With a running time of O(n^2), it is highly inefficient for large data sets. ● Additionally, the presence of turtles can severely slow the sort.