SlideShare ist ein Scribd-Unternehmen logo
1 von 85
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
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
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

Weitere ähnliche Inhalte

Andere mochten auch (9)

Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Binary code decimal Adder
Binary code decimal AdderBinary code decimal Adder
Binary code decimal Adder
 
The selection sort algorithm
The selection sort algorithmThe selection sort algorithm
The selection sort algorithm
 
Bubble sort algorithm
Bubble sort algorithmBubble sort algorithm
Bubble sort algorithm
 
BCD ADDER
BCD ADDER BCD ADDER
BCD ADDER
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
BCD Adder
BCD AdderBCD Adder
BCD Adder
 

Ähnlich wie Bubble sort (17)

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
 
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
 
BUBBLESORT
BUBBLESORT BUBBLESORT
BUBBLESORT
 
killing camp 주차장 나누기-2 topology sort.pdf
killing camp 주차장 나누기-2 topology sort.pdfkilling camp 주차장 나누기-2 topology sort.pdf
killing camp 주차장 나누기-2 topology sort.pdf
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.
 
Ropa Flash2
Ropa Flash2Ropa Flash2
Ropa Flash2
 
Ropa Flash1
Ropa Flash1Ropa Flash1
Ropa Flash1
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10
 
Kleider Flash2
Kleider Flash2Kleider Flash2
Kleider Flash2
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
Radix sort
Radix sortRadix sort
Radix sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 

Mehr von Ali Asgher (8)

hadith sciences(mine)
hadith sciences(mine)hadith sciences(mine)
hadith sciences(mine)
 
struggle for pakistan
struggle for pakistanstruggle for pakistan
struggle for pakistan
 
contents of holy quran
contents of holy qurancontents of holy quran
contents of holy quran
 
downfall Of Muslims In India
downfall Of Muslims In Indiadownfall Of Muslims In India
downfall Of Muslims In India
 
2.muslim rule in india
2.muslim rule in india2.muslim rule in india
2.muslim rule in india
 
Divine Communications
Divine CommunicationsDivine Communications
Divine Communications
 
Guidelines for project
Guidelines for projectGuidelines for project
Guidelines for project
 
Religious consincence
Religious consincenceReligious consincence
Religious consincence
 

Kürzlich hochgeladen

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

Kürzlich hochgeladen (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Bubble sort

  • 2. 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
  • 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 512354277 101 1 2 3 4 5 6
  • 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 512354277 101 1 2 3 4 5 6 Swap42 77
  • 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 512357742 101 1 2 3 4 5 6 Swap35 77
  • 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 512773542 101 1 2 3 4 5 6 Swap12 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 577123542 101 1 2 3 4 5 6 No need to swap
  • 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 577123542 101 1 2 3 4 5 6 Swap5 101
  • 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 77123542 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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”
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. 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
  • 56. 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
  • 57. 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
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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
  • 62. 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
  • 63. 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
  • 64. 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
  • 65. 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
  • 66. 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”
  • 67. 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
  • 68. 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
  • 69. 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
  • 70. 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
  • 71. 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
  • 72. 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
  • 73. 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
  • 74. 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
  • 75. 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
  • 76. 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”
  • 77. 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
  • 78. 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
  • 79. 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
  • 80. 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
  • 81. 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
  • 82. 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
  • 83. 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”
  • 84. 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.
  • 85. 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