SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à Bubble sort

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
 
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
 

Similaire à Bubble sort (15)

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
 
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
 
Sorting
SortingSorting
Sorting
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10
 

Dernier

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Dernier (20)

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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

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