SlideShare a Scribd company logo
1 of 101
Bubble Sort/Exchange
Sort Algorithm
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
"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
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
Why (N-1) times
?
Explorer Yourself
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
Already Sorted Collections?
• What if the collection was already sorted?
• What if only a few elements were out of place and
after a couple of “bubble ups,” the collection was
sorted?
• We want to be able to detect this
and “stop early”!
4235125 77
1 2 3 4 5 6
101
Using a Boolean “Flag”
• We can use a boolean variable to determine if any
swapping occurred during the “bubble up.”
• If no swapping occurred, then we know that the
collection is already sorted!
• This boolean “flag” needs to be reset after each
“bubble up.”
did_swap isoftype Boolean
did_swap <- true
loop
exitif ((to_do = 0) OR NOT(did_swap))
index <- 1
did_swap <- false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap <- true
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8 did_swap false
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap false
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap true
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8 did_swap true
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8 did_swap true
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8 did_swap true
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8 did_swap true
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8 did_swap true
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
An Animated Example
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
After First Pass of Outer Loop
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
7
8
N 8
Finished first “Bubble Up”
did_swap true
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
No Swap
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
Swap
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap true
Swap
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
No Swap
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
The Second “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
After Second Pass of Outer Loop
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
6
7
N 8 did_swap true
Finished second “Bubble Up”
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
Swap
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap true
Swap
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
No Swap
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
The Third “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
After Third Pass of Outer Loop
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
6
N 8 did_swap true
Finished third “Bubble Up”
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
Swap
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap true
Swap
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
No Swap
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
No Swap
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
No Swap
After Fourth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
5
N 8 did_swap true
Finished fourth “Bubble Up”
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
No Swap
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
No Swap
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
No Swap
After Fifth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
Finished “Early”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
4
N 8 did_swap false
We didn’t do any swapping,
so all of the other elements
must be correctly placed.
We can “skip” the last two
passes of the outer loop.
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
– Can finish early if no swapping occurs
• We reduce the number of elements we
compare each time one is correctly placed
Truth in CS Act
• NOBODY EVER USES BUBBLE SORT
• NOBODY
• NOT EVER
• BECAUSE IT IS EXTREMELY INEFFICIENT
Questions?

More Related Content

Similar to Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it 3-morning-data structures and algorithms-2018,bs cs 3-morning-data structures and algorithms-2018 -bubble sort-bubble sort

Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort PythonValneyFilho1
 
Sorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptSorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptsaadpisjes
 
sorting techniques PPT.ppt
sorting techniques PPT.pptsorting techniques PPT.ppt
sorting techniques PPT.pptAnilVastav
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.NikhilSoni177492
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptxTusharTikia
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10PDS
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubbleirdginfo
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sortNauman Ali
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithmsZaid Hameed
 
No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]
No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]
No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]Matthew Philip
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 

Similar to Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it 3-morning-data structures and algorithms-2018,bs cs 3-morning-data structures and algorithms-2018 -bubble sort-bubble sort (20)

cs1311lecture16wdl.ppt
cs1311lecture16wdl.pptcs1311lecture16wdl.ppt
cs1311lecture16wdl.ppt
 
Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort Python
 
Bubble Sort.ppt
Bubble Sort.pptBubble Sort.ppt
Bubble Sort.ppt
 
Sorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptSorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .ppt
 
sorting techniques PPT.ppt
sorting techniques PPT.pptsorting techniques PPT.ppt
sorting techniques PPT.ppt
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.
 
BUBBLESORT
BUBBLESORT BUBBLESORT
BUBBLESORT
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptx
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)DESIGN AND ANALYSIS OF ALGORITHM (DAA)
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubble
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]
No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]
No (Lab) Jacket Required: Designing Experiments for Learning [2019 Flowcon]
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 

Recently uploaded

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 

Recently uploaded (20)

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

Bubble sort/ Exchange sort Algorithmdata structures and algorithms-2018,bs it 3-morning-data structures and algorithms-2018,bs cs 3-morning-data structures and algorithms-2018 -bubble sort-bubble sort

  • 3.
  • 4. 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
  • 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. 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
  • 13. 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
  • 14. 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
  • 15. 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.
  • 16. “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
  • 23. Already Sorted Collections? • What if the collection was already sorted? • What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? • We want to be able to detect this and “stop early”! 4235125 77 1 2 3 4 5 6 101
  • 24. Using a Boolean “Flag” • We can use a boolean variable to determine if any swapping occurred during the “bubble up.” • If no swapping occurred, then we know that the collection is already sorted! • This boolean “flag” needs to be reset after each “bubble up.”
  • 25. did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop
  • 26. An Animated Example 674523 14 6 3398 42 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true
  • 27. An Animated Example 674523 14 6 3398 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 did_swap false
  • 28. An Animated Example 674523 14 6 3398 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap false
  • 29. An Animated Example 674598 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true
  • 30. An Animated Example 674598 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 did_swap true
  • 31. An Animated Example 674598 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 32. An Animated Example 679845 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 33. An Animated Example 679845 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 did_swap true
  • 34. An Animated Example 679845 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 35. An Animated Example 671445 98 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 36. An Animated Example 671445 98 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 did_swap true
  • 37. An Animated Example 671445 98 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 38. An Animated Example 671445 6 98 3323 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 39. An Animated Example 671445 6 98 3323 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 did_swap true
  • 40. An Animated Example 671445 6 98 3323 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 41. An Animated Example 981445 6 67 3323 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 42. An Animated Example 981445 6 67 3323 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 did_swap true
  • 43. An Animated Example 981445 6 67 3323 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 44. An Animated Example 331445 6 67 9823 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 45. An Animated Example 331445 6 67 9823 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true
  • 46. An Animated Example 331445 6 67 9823 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 47. An Animated Example 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 48. After First Pass of Outer Loop 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 7 8 N 8 Finished first “Bubble Up” did_swap true
  • 49. The Second “Bubble Up” 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false
  • 50. The Second “Bubble Up” 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false No Swap
  • 51. The Second “Bubble Up” 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false
  • 52. The Second “Bubble Up” 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false Swap
  • 53. The Second “Bubble Up” 334514 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap true Swap
  • 54. The Second “Bubble Up” 334514 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true
  • 55. The Second “Bubble Up” 334514 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap
  • 56. The Second “Bubble Up” 33614 45 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap
  • 57. The Second “Bubble Up” 33614 45 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true
  • 58. The Second “Bubble Up” 33614 45 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true No Swap
  • 59. The Second “Bubble Up” 33614 45 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true
  • 60. The Second “Bubble Up” 33614 45 67 4223 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap
  • 61. The Second “Bubble Up” 67614 45 33 4223 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap
  • 62. The Second “Bubble Up” 67614 45 33 4223 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true
  • 63. The Second “Bubble Up” 67614 45 33 4223 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap
  • 64. The Second “Bubble Up” 42614 45 33 6723 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap
  • 65. After Second Pass of Outer Loop 42614 45 33 6723 98 1 2 3 4 5 6 7 8 to_do index 6 7 N 8 did_swap true Finished second “Bubble Up”
  • 66. The Third “Bubble Up” 42614 45 33 6723 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false
  • 67. The Third “Bubble Up” 42614 45 33 6723 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false Swap
  • 68. The Third “Bubble Up” 42623 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap true Swap
  • 69. The Third “Bubble Up” 42623 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true
  • 70. The Third “Bubble Up” 42623 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap
  • 71. The Third “Bubble Up” 42236 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap
  • 72. The Third “Bubble Up” 42236 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true
  • 73. The Third “Bubble Up” 42236 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true No Swap
  • 74. The Third “Bubble Up” 42236 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true
  • 75. The Third “Bubble Up” 42236 45 33 6714 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap
  • 76. The Third “Bubble Up” 42236 33 45 6714 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap
  • 77. The Third “Bubble Up” 42236 33 45 6714 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true
  • 78. The Third “Bubble Up” 42236 33 45 6714 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap
  • 79. The Third “Bubble Up” 45236 33 42 6714 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap
  • 80. After Third Pass of Outer Loop 45236 33 42 6714 98 1 2 3 4 5 6 7 8 to_do index 5 6 N 8 did_swap true Finished third “Bubble Up”
  • 81. The Fourth “Bubble Up” 45236 33 42 6714 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false
  • 82. The Fourth “Bubble Up” 45236 33 42 6714 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false Swap
  • 83. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap true Swap
  • 84. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true
  • 85. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true No Swap
  • 86. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true
  • 87. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true No Swap
  • 88. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true
  • 89. The Fourth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true No Swap
  • 90. After Fourth Pass of Outer Loop 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 4 5 N 8 did_swap true Finished fourth “Bubble Up”
  • 91. The Fifth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false
  • 92. The Fifth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false No Swap
  • 93. The Fifth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false
  • 94. The Fifth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false No Swap
  • 95. The Fifth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false
  • 96. The Fifth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false No Swap
  • 97. After Fifth Pass of Outer Loop 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false Finished fifth “Bubble Up”
  • 98. Finished “Early” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop.
  • 99. 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 – Can finish early if no swapping occurs • We reduce the number of elements we compare each time one is correctly placed
  • 100. Truth in CS Act • NOBODY EVER USES BUBBLE SORT • NOBODY • NOT EVER • BECAUSE IT IS EXTREMELY INEFFICIENT