SlideShare a Scribd company logo
Bubble sort
Bubble sort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
512354277 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
Exchange/Bubble Sort:
It uses simple algorithm. It sorts by comparing each pair
of adjacent items and swapping them in the order. This
will be repeated until no swaps are needed. The
algorithm got its name from the way smaller elements
"bubble" to the top of the list.
It is not that much efficient, when a list is having more
than a few elements. Among simple sorting algorithms,
algorithms like insertion sort are usually considered as
more efficient.
Bubble sort is little slower compared to other sorting
techniques but it is easy because it deals with only two
elements at a time.
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
512354277 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
512354277 101
1 2 3 4 5 6
Swap42 77
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
512357742 101
1 2 3 4 5 6
Swap35 77
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
512773542 101
1 2 3 4 5 6
Swap12 77
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
577123542 101
1 2 3 4 5 6
No need to swap
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
577123542 101
1 2 3 4 5 6
Swap5 101
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
Bubble Sort
1. In each pass, we compare adjacent elements and swap them if they are
out of order until the end of the list. By doing so, the 1st
pass ends up
“bubbling up” the largest element to the last position on the list
2. The 2nd pass bubbles up the 2nd
largest, and so on until, after N-1
passes, the list is sorted.
Example:
Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17
45 89 | 68 90 29 34 17 45 68 | 89 29 34 17
68 89 | 90 29 34 17 68 89 | 29 34 17
89 90 | 29 34 17 29 89 | 34 17
29 90 | 34 17 34 89 | 17
34 90 | 17 17 89
17 90
45 68 89 29 34 17 90 45 68 29 34 17 89
largest 2nd
largest
The “Bubble Up” Algorithm
index <- 1
last_compare_at <- n – 1
loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
No, Swap isn’t built in.
Procedure Swap(a, b isoftype in/out Num)
t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap
LB
Items of Interest
• Notice that only the largest value is
correctly placed
• All other values are still out of order
• So we need to repeat this process
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an element,
we place it in its correct location…
• Then we repeat the “bubble up”
process N – 1 times.
• This guarantees we’ll correctly
place all N elements.
“Bubbling” All the Elements
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
4235125 77
1 2 3 4 5 6
101
N-1
Reducing the Number of Comparisons
12354277 101
1 2 3 4 5 6
5
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
Reducing the Number of Comparisons
• On the Nth
“bubble up”, we only need to
do MAX-N comparisons.
• For example:
– This is the 4th
“bubble up”
– MAX is 6
– Thus we have 2 comparisons to do
4253512 77
1 2 3 4 5 6
101
Putting It All Together
N is … // Size of Array
Arr_Type definesa Array[1..N] of Num
Procedure Swap(n1, n2 isoftype in/out Num)
temp isoftype Num
temp <- n1
n1 <- n2
n2 <- temp
endprocedure // Swap
procedure Bubblesort(A isoftype in/out Arr_Type)
to_do, index isoftype Num
to_do <- N – 1
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Innerloop
Outerloop
Bubble sort
Summary
• “Bubble Up” algorithm will move largest
value to its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed:
– Maximum of N-1 times
• We reduce the number of elements we
compare each time one is correctly placed
Thank You

More Related Content

What's hot (20)

PPTX
Queue ppt
SouravKumar328
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPTX
Quick sort
Afaq Mansoor Khan
 
PPTX
STACKS IN DATASTRUCTURE
Archie Jamwal
 
PPTX
Doubly Linked List
Ninad Mankar
 
PPTX
Sorting
Ashim Lamichhane
 
PPT
Queue data structure
anooppjoseph
 
PPTX
Searching and sorting
PoojithaBollikonda
 
PPTX
Binary Search Tree
sagar yadav
 
PPTX
queue & its applications
somendra kumar
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPT
Lec 17 heap data structure
Sajid Marwat
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PPSX
Data Structure (Queue)
Adam Mukharil Bachtiar
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PPTX
Queue in Data Structure
Janki Shah
 
PPT
Binary Search
kunj desai
 
Queue ppt
SouravKumar328
 
Sorting Algorithms
Pranay Neema
 
sorting and its types
SIVASHANKARIRAJAN
 
Quick sort
Afaq Mansoor Khan
 
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Doubly Linked List
Ninad Mankar
 
Queue data structure
anooppjoseph
 
Searching and sorting
PoojithaBollikonda
 
Binary Search Tree
sagar yadav
 
queue & its applications
somendra kumar
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Merge sort algorithm
Shubham Dwivedi
 
Priority Queue in Data Structure
Meghaj Mallick
 
Lec 17 heap data structure
Sajid Marwat
 
Quick sort-Data Structure
Jeanie Arnoco
 
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Binary Tree Traversal
Dhrumil Panchal
 
Queue in Data Structure
Janki Shah
 
Binary Search
kunj desai
 

Viewers also liked (9)

PDF
Sorting
Kariman Karm Gabaa
 
PPTX
Bubble sort algorithm
David Burks-Courses
 
PPT
Bubble sort a best presentation topic
Saddam Hussain
 
PDF
Bubblesort Algorithm
Tobias Straub
 
PPT
3.1 bubble sort
Krish_ver2
 
PPT
Sorting
Ghaffar Khan
 
PPTX
Bubble Sort
geeortiz
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PPT
Chemistry - Chp 4 - Atomic Structure - PowerPoint
Mr. Walajtys
 
Bubble sort algorithm
David Burks-Courses
 
Bubble sort a best presentation topic
Saddam Hussain
 
Bubblesort Algorithm
Tobias Straub
 
3.1 bubble sort
Krish_ver2
 
Sorting
Ghaffar Khan
 
Bubble Sort
geeortiz
 
Data Structures - Searching & sorting
Kaushal Shah
 
Chemistry - Chp 4 - Atomic Structure - PowerPoint
Mr. Walajtys
 
Ad

Similar to Bubble sort (20)

PPTX
bubblesort-130417100323-phpapp02[1].pptx
TusharTikia
 
PPTX
Bubble Sort presentation.pptx
TusharTikia
 
PPTX
BUBBLESORT
Ashish Sadavarti
 
PPT
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Muhammad Abuzar
 
PPT
Bubble Sort.ppt
MuhammadSheraz836877
 
PPT
cs1311lecture16wdl.ppt
RameshKumarYadav29
 
PPT
Bubble Sort Python
ValneyFilho1
 
PPTX
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
ArjayBalberan1
 
PPT
Data Structure and Algorithms Sorting
ManishPrajapati78
 
PPT
ds 3Sorting.ppt
AlliVinay1
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PPT
sortingofarrays-240425134605-b7961d41.ppt
YasirAli288
 
PPT
Sorting of arrays, types in c++ .IST .ppt
saadpisjes
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPT
Bubble_sort week Bubble_sort week Bubble_sort week
Aman970290
 
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPTX
Unit vii sorting
Tribhuvan University
 
bubblesort-130417100323-phpapp02[1].pptx
TusharTikia
 
Bubble Sort presentation.pptx
TusharTikia
 
BUBBLESORT
Ashish Sadavarti
 
Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it...
Muhammad Abuzar
 
Bubble Sort.ppt
MuhammadSheraz836877
 
cs1311lecture16wdl.ppt
RameshKumarYadav29
 
Bubble Sort Python
ValneyFilho1
 
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
ArjayBalberan1
 
Data Structure and Algorithms Sorting
ManishPrajapati78
 
ds 3Sorting.ppt
AlliVinay1
 
L 14-ct1120
Zia Ush Shamszaman
 
sortingofarrays-240425134605-b7961d41.ppt
YasirAli288
 
Sorting of arrays, types in c++ .IST .ppt
saadpisjes
 
Unit 7 sorting
Dabbal Singh Mahara
 
Bubble_sort week Bubble_sort week Bubble_sort week
Aman970290
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Radhika Talaviya
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
Unit vii sorting
Tribhuvan University
 
Ad

Recently uploaded (20)

PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 

Bubble sort

  • 3. Sorting • Sorting takes an unordered collection and makes it an ordered one. 512354277 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 4. Exchange/Bubble Sort: It uses simple algorithm. It sorts by comparing each pair of adjacent items and swapping them in the order. This will be repeated until no swaps are needed. The algorithm got its name from the way smaller elements "bubble" to the top of the list. It is not that much efficient, when a list is having more than a few elements. Among simple sorting algorithms, algorithms like insertion sort are usually considered as more efficient. Bubble sort is little slower compared to other sorting techniques but it is easy because it deals with only two elements at a time.
  • 5. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 512354277 101 1 2 3 4 5 6
  • 6. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 512354277 101 1 2 3 4 5 6 Swap42 77
  • 7. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 512357742 101 1 2 3 4 5 6 Swap35 77
  • 8. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 512773542 101 1 2 3 4 5 6 Swap12 77
  • 9. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 577123542 101 1 2 3 4 5 6 No need to swap
  • 10. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 577123542 101 1 2 3 4 5 6 Swap5 101
  • 11. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 77123542 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 12. Bubble Sort 1. In each pass, we compare adjacent elements and swap them if they are out of order until the end of the list. By doing so, the 1st pass ends up “bubbling up” the largest element to the last position on the list 2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1 passes, the list is sorted. Example: Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17 45 89 | 68 90 29 34 17 45 68 | 89 29 34 17 68 89 | 90 29 34 17 68 89 | 29 34 17 89 90 | 29 34 17 29 89 | 34 17 29 90 | 34 17 34 89 | 17 34 90 | 17 17 89 17 90 45 68 89 29 34 17 90 45 68 29 34 17 89 largest 2nd largest
  • 13. The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop
  • 14. No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) t isoftype Num t <- a a <- b b <- t endprocedure // Swap LB
  • 15. Items of Interest • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 77123542 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 16. Repeat “Bubble Up” How Many Times? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements.
  • 17. “Bubbling” All the Elements 77123542 5 1 2 3 4 5 6 101 5421235 77 1 2 3 4 5 6 101 4253512 77 1 2 3 4 5 6 101 4235512 77 1 2 3 4 5 6 101 4235125 77 1 2 3 4 5 6 101 N-1
  • 18. Reducing the Number of Comparisons 12354277 101 1 2 3 4 5 6 5 77123542 5 1 2 3 4 5 6 101 5421235 77 1 2 3 4 5 6 101 4253512 77 1 2 3 4 5 6 101 4235512 77 1 2 3 4 5 6 101
  • 19. Reducing the Number of Comparisons • On the Nth “bubble up”, we only need to do MAX-N comparisons. • For example: – This is the 4th “bubble up” – MAX is 6 – Thus we have 2 comparisons to do 4253512 77 1 2 3 4 5 6 101
  • 20. Putting It All Together
  • 21. N is … // Size of Array Arr_Type definesa Array[1..N] of Num Procedure Swap(n1, n2 isoftype in/out Num) temp isoftype Num temp <- n1 n1 <- n2 n2 <- temp endprocedure // Swap
  • 22. procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort Innerloop Outerloop
  • 24. Summary • “Bubble Up” algorithm will move largest value to its correct location (to the right) • Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times • We reduce the number of elements we compare each time one is correctly placed