SlideShare uma empresa Scribd logo
1 de 93
Bubble Sort
Prepared By:
Rao Muhammad Salman
MCS
The Islamia University BahawalPur(Pakistan)
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6 7 8
674523 14 6 3398 42
1 2 3 4 5 6 7 8
452314 33 42 676 98
"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
1 2 3 4 5 6 7 8
674523 14 6 3398 42
"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
Swap
1 2 3 4 5 6 7 8
674598 14 6 3323 42
Value swapped
"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
Swap
1 2 3 4 5 6 7 8
679845 14 6 3323 42
Value swapped
"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
Swap
1 2 3 4 5 6 7 8
671445 98 6 3323 42
Value swapped
"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
Value swapped
Swap
1 2 3 4 5 6 7 8
671445 6 98 3323 42
"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
Swap
1 2 3 4 5 6 7 8
981445 6 67 3323 42
Value swapped
"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
Swap
1 2 3 4 5 6 7 8
331445 6 67 9823 42
Value swapped
"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
Swap
1 2 3 4 5 6 7 8
331445 6 67 4223 98
Value swapped
"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
Largest value correctly placed
1 2 3 4 5 6 7 8
331445 6 67 4223 98
The “Bubble Up” Algorithm
index <- 1
last_compare_at <- n – 1
loopouter
exitif(index > last_compare_at)
Loopinner
to_do<-0
exitif(to_do > index)
if(A[to_do] > A[to_do + 1]) then
Swap(A[to_do], A[to_do + 1])
endif
to_do<- to_do + 1
endloopinner
index <- index - 1
endloopouter
No, Swap isn’t built in.
Procedure Swap(a, b isoftype in/out Num)
temp isoftype Num
temp <- a
a <- b
b <- temp
endprocedure // Swap
Bubble Sort Code
void bubble_sort( int data[ ], int size)
{
int outer, inner,temp;
for(outer = size-1; outer =>0; --outer)
{
for(inner = 0; inner < outer;++inner)
if(data[inner] > data[inner+1])
{
temp = data[inner];
data[inner] = data[inner+1];
data[inner+1] = temp;
}
}
}
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
Largest value correctly placed
1 2 3 4 5 6 7 8
331445 6 67 4223 98
Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an element, we
place it in its largest correct location…
• Then we repeat InnerLoop the “bubble up”
process OuterLoop – 1 times.
• This guarantees we’ll correctly
place all N elements.
Dry Run
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
The First “Bubble Up”
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8 did_swap false
The First “Bubble Up”
The First “Bubble Up”
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap false
The First “Bubble Up”
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap true
The First “Bubble Up”
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8 did_swap true
The First “Bubble Up”
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
The First “Bubble Up”
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
The First “Bubble Up”
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8 did_swap true
The First “Bubble Up”
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
The First “Bubble Up”
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
The First “Bubble Up”
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8 did_swap true
The First “Bubble Up”
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
The First “Bubble Up”
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
The First “Bubble Up”
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8 did_swap true
The First “Bubble Up”
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
The First “Bubble Up”
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
The First “Bubble Up”
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8 did_swap true
The First “Bubble Up”
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
The First “Bubble Up”
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
The First “Bubble Up”
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
The First “Bubble Up”
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
The First “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
Finished 1st “Bubble Up”
No Swap
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”
42614 45 33 6723 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
Finished 2nd “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
Finished 3rd “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
Finished 4th “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
Finished 5th “Bubble Up”
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
1
N 8 did_swap false
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
1
N 8 did_swap false
No Swap
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
2
N 8 did_swap false
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
2
N 8 did_swap false
No Swap
Finished 6th “Bubble Up”
The Seventh “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
1
1
N 8 did_swap false
The Seventh “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
1
1
N 8 did_swap false
No Swap
Finished 7th “Bubble Up”
Sorted Elements
452314 33 42 676 98
1 2 3 4 5 6 7 8
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
Bubble sort

Mais conteúdo relacionado

Semelhante a Bubble sort

Bubble Sort Python
Bubble Sort PythonBubble Sort Python
Bubble Sort PythonValneyFilho1
 
sorting techniques PPT.ppt
sorting techniques PPT.pptsorting techniques PPT.ppt
sorting techniques PPT.pptAnilVastav
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptxTusharTikia
 
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
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10PDS
 

Semelhante a Bubble sort (13)

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 techniques PPT.ppt
sorting techniques PPT.pptsorting techniques PPT.ppt
sorting techniques PPT.ppt
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
BUBBLESORT
BUBBLESORT BUBBLESORT
BUBBLESORT
 
Bubble Sort.pptx
Bubble Sort.pptxBubble Sort.pptx
Bubble Sort.pptx
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptx
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10
 

Último

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 

Último (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 

Bubble sort

  • 1. Bubble Sort Prepared By: Rao Muhammad Salman MCS The Islamia University BahawalPur(Pakistan)
  • 2. Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 7 8 674523 14 6 3398 42 1 2 3 4 5 6 7 8 452314 33 42 676 98
  • 3. "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 1 2 3 4 5 6 7 8 674523 14 6 3398 42
  • 4. "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 Swap 1 2 3 4 5 6 7 8 674598 14 6 3323 42 Value swapped
  • 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 Swap 1 2 3 4 5 6 7 8 679845 14 6 3323 42 Value swapped
  • 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 Swap 1 2 3 4 5 6 7 8 671445 98 6 3323 42 Value swapped
  • 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 Value swapped Swap 1 2 3 4 5 6 7 8 671445 6 98 3323 42
  • 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 Swap 1 2 3 4 5 6 7 8 981445 6 67 3323 42 Value swapped
  • 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 Swap 1 2 3 4 5 6 7 8 331445 6 67 9823 42 Value swapped
  • 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 Swap 1 2 3 4 5 6 7 8 331445 6 67 4223 98 Value swapped
  • 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 Largest value correctly placed 1 2 3 4 5 6 7 8 331445 6 67 4223 98
  • 12. The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loopouter exitif(index > last_compare_at) Loopinner to_do<-0 exitif(to_do > index) if(A[to_do] > A[to_do + 1]) then Swap(A[to_do], A[to_do + 1]) endif to_do<- to_do + 1 endloopinner index <- index - 1 endloopouter
  • 13. No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) temp isoftype Num temp <- a a <- b b <- temp endprocedure // Swap
  • 14. Bubble Sort Code void bubble_sort( int data[ ], int size) { int outer, inner,temp; for(outer = size-1; outer =>0; --outer) { for(inner = 0; inner < outer;++inner) if(data[inner] > data[inner+1]) { temp = data[inner]; data[inner] = data[inner+1]; data[inner+1] = temp; } } }
  • 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 Largest value correctly placed 1 2 3 4 5 6 7 8 331445 6 67 4223 98
  • 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 largest correct location… • Then we repeat InnerLoop the “bubble up” process OuterLoop – 1 times. • This guarantees we’ll correctly place all N elements.
  • 18. 674523 14 6 3398 42 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true The First “Bubble Up”
  • 19. 674523 14 6 3398 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 did_swap false The First “Bubble Up”
  • 20. The First “Bubble Up” 674523 14 6 3398 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap false
  • 21. The First “Bubble Up” 674598 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true
  • 22. The First “Bubble Up” 674598 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 did_swap true
  • 23. The First “Bubble Up” 674598 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 24. The First “Bubble Up” 679845 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 25. The First “Bubble Up” 679845 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 did_swap true
  • 26. The First “Bubble Up” 679845 14 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 27. The First “Bubble Up” 671445 98 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 28. The First “Bubble Up” 671445 98 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 did_swap true
  • 29. The First “Bubble Up” 671445 98 6 3323 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 30. The First “Bubble Up” 671445 6 98 3323 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 31. The First “Bubble Up” 671445 6 98 3323 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 did_swap true
  • 32. The First “Bubble Up” 671445 6 98 3323 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 33. The First “Bubble Up” 981445 6 67 3323 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 34. The First “Bubble Up” 981445 6 67 3323 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 did_swap true
  • 35. The First “Bubble Up” 981445 6 67 3323 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 36. The First “Bubble Up” 331445 6 67 9823 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 37. The First “Bubble Up” 331445 6 67 9823 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true
  • 38. The First “Bubble Up” 331445 6 67 9823 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 39. The First “Bubble Up” 331445 6 67 4223 98 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true Finished 1st “Bubble Up” No Swap
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. 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 Finished 2nd “Bubble Up”
  • 56. 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
  • 57. 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
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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
  • 62. 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
  • 63. 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
  • 64. 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
  • 65. 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
  • 66. 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
  • 67. 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
  • 68. 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
  • 69. 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 Finished 3rd “Bubble Up”
  • 70. 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
  • 71. 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
  • 72. 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
  • 73. 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
  • 74. 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
  • 75. 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
  • 76. 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
  • 77. 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
  • 78. 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 Finished 4th “Bubble Up”
  • 79. 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
  • 80. 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
  • 81. 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
  • 82. 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
  • 83. 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
  • 84. 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 Finished 5th “Bubble Up”
  • 85. The Sixth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 2 1 N 8 did_swap false
  • 86. The Sixth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 2 1 N 8 did_swap false No Swap
  • 87. The Sixth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 2 2 N 8 did_swap false
  • 88. The Sixth “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 2 2 N 8 did_swap false No Swap Finished 6th “Bubble Up”
  • 89. The Seventh “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 1 1 N 8 did_swap false
  • 90. The Seventh “Bubble Up” 452314 33 42 676 98 1 2 3 4 5 6 7 8 to_do index 1 1 N 8 did_swap false No Swap Finished 7th “Bubble Up”
  • 91. Sorted Elements 452314 33 42 676 98 1 2 3 4 5 6 7 8
  • 92. 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