SlideShare une entreprise Scribd logo
1  sur  52
Télécharger pour lire hors ligne
BUBBLEBUBBLE
SORTSORT
tobiasstraub.com
Bubble Sort
is a very simple sort algorithm
tobiasstraub.com
Ah, okay
and how does the process work?
tobiasstraub.com
Let us see
we want to sort the following data
sequence
seqA = 6 | 8 | 10 | 3
tobiasstraub.com
Let's look
at the first two numbers
seqA = 6 | 8 | 10 | 3
6 8
Okay! Now the question is
Is the second number (8) smaller then
the first (6)? Answer: NO, it is not
tobiasstraub.com
Perfect,
then we need to do nothing in this step
tobiasstraub.com
Okay,
our seqA has not changed.
The next step is to perform the same
check again, but this time we move our
pointers around a number to the right
tobiasstraub.com
A number
to the right
seqA = 6 | 8 | 10 | 3
8 10
Okay! Now the question is again
Is the second number (10) smaller then
the first (8)? Answer: NO, it is not
tobiasstraub.com
Perfect,
then we need to do nothing in this step
again
tobiasstraub.com
Good,
then let us again move a number to the
right
tobiasstraub.com
A number
to the right
seqA = 6 | 8 | 10 | 3
10 3
Okay! Now the question is again
Is the second number (3) smaller then
the first (10)? Answer: YES, it is
tobiasstraub.com
Now,
we have to swap the numbers
seqA = 6 | 8 | 10 | 3
10 3
seqA = 6 | 8 | 3 | 10
tobiasstraub.com
Awesome!
So this step is completed
tobiasstraub.com
What have we achieved?
The data sequence is now completely
pass through, so that the largest element
is at the end of the data sequence
tobiasstraub.com
Now what?
Now we pass through the sequence of
data once again, so that the second
largest number (8) is on the second last
position
At the last position of our data sequence
we have already the largest number (10)
tobiasstraub.com
How do we do that?
It's easy! - We take our previously sorted
data sequence and complete all the steps
again
tobiasstraub.com
Let's look
at the first two numbers
seqA = 6 | 8 | 3 | 10
6 8
Okay! Now the question is
Is the second number (8) smaller then
the first (6)? Answer: NO, it is not
tobiasstraub.com
Perfect,
then we need to do nothing in this step
tobiasstraub.com
Good,
then let us move a number to the
right
tobiasstraub.com
A number
to the right
seqA = 6 | 8 | 3 | 10
8 3
Okay! Now the question is again
Is the second number (3) smaller then
the first (8)? Answer: YES, it is
tobiasstraub.com
Now,
we have to swap the numbers
seqA = 6 | 8 | 3 | 10
8 3
seqA = 6 | 3 | 8 | 10
tobiasstraub.com
Good,
the last number we may exclude from the
comparison.
We remember: In the first pass we have
already promoted the largest number to
the last position
tobiasstraub.com
What have we achieved?
The data sequence has now been rerun
completely, so that the second largest
number is at the second last position
of the data sequence
tobiasstraub.com
Okay, but what's next?
Guess what!
tobiasstraub.com
Correctly!
We take our previously sorted
data sequence and complete all the steps
again
tobiasstraub.com
Let's look
at the first two numbers
seqA = 6 | 3 | 8 | 10
6 3
Okay! Now the question is
Is the second number (3) smaller then
the first (6)? Answer: YES, it is
tobiasstraub.com
Now,
we have to swap the numbers
seqA = 6 | 3 | 8 | 10
6 3
seqA = 3 | 6 | 8 | 10
tobiasstraub.com
Very well,
the second last and the last element we
may exclude from the comparison.
We remember: In the first and the second
pass we have already promoted the
largest number to the last position and the
second last number to the second last
position
tobiasstraub.com
Yay!
That was all.
We have reached the end of the
sequence
tobiasstraub.com
You could see,
that there is a very simple algorithm for
for sorting elements
tobiasstraub.com
And you know what?
This is even the optimized version of
bubblesort
tobiasstraub.com
In the traditional
version of the algorithm, all comparisons
are made for each run. It does not matter
whether the elements are already in the
correct position
tobiasstraub.com
Let's talk about complexity
Let us consider in terms of complexity
at first the traditional algorithm
tobiasstraub.com
worst case O(n²)
If we want to bring a number to the
desired position, we need in the worst
case n-1 comparisons
tobiasstraub.com
worst case O(n²)
In our example, we had a data sequence
with four elements
seqA = 6 | 8 | 10 | 3
Do you remember?
tobiasstraub.com
worst case O(n²)
Okay, in the worst case, we need to
perform three comparisons, because
seqA has four elements 4 – 1 = 3 (n-1)
seqA = 6 | 8 | 10 | 3
1 : Compare 6 with 8
2 : Compare 8 with 10
3 : Compare 10 with 3
tobiasstraub.com
worst case O(n²)
So, now we have one number on the
desired position
The question we must ask ourselves
now is
How many times must we repeat
this procedure in the worst case, so that
all our numbers are in the correct
position?
tobiasstraub.com
worst case O(n²)
It's logical! - Until all the numbers have
reached the correct position
In the worst case, the n-1 passes
tobiasstraub.com
worst case O(n²)
The O-notation for the worst case,
given by the number of passes
multiplied by the number of comparisons
(n-1) * (n-1) = O(n²)
Thus we have a quadratic term, which
makes the bubblesort algorithm with
increasing number of data extremely
inefficient
tobiasstraub.com
best case O(n)
The best case would be if the data is
already completely stored
tobiasstraub.com
For example
We have the following data sequence
seqB = 3 | 6 | 8 | 10
We pass through the data sequence.
At the end of the pass we would notice
that there was no swapping. Thus, the
data sequence is already stored.
tobiasstraub.com
The big O
The O-notation for the best case,
given by the number of passes
multiplied by the number of comparisons
1 * (n-1)
tobiasstraub.com
Okay, let's see now
how the complexity behaves in Bubblesort
optimized version
tobiasstraub.com
As we know,
we can at eatch iteration of the data
sequence save one comparison
(namely comparison with the already
sorted number from the last run)
tobiasstraub.com
The number of comparisons..
seqA = 6 | 8 | 10 | 3
Pass 1: 6 | 8 | 3 | 10 (3 comparisons: n-1)
Pass 2: 6 | 3 | 8 | 10 (2 comparisons: n-2)
Pass 3: 3 | 6 | 8 | 10 (1 comparisons: n-3)
Pass 4: 3 | 6 | 8 | 10 (0 comparisons: 1)
..can thus be described as follows:
(n-1) + (n-2) + … + 1 also n/2
(linear O-notation)
tobiasstraub.com
The number of passes
will not change
tobiasstraub.com
The O-notation
for the optimized algorithm,
thus obtained again by the number of
passes multiplied by the number of
comparisons
(n-1) * (n/2) = O(n²)
Thus we have again a quadratic term,
but in terms of the linear portion
(comparisons) much faster on the run time
tobiasstraub.com
Okay, now
a few facts about Bubblesort
tobiasstraub.com
Bubblesort..
..is hardly used in practice.
..has a bad runtime behavior.
..is very easy to understand.
tobiasstraub.com
Code? Now!
You can download the code discussed
here (Java)
Optimized Version
http://tobiasstraub.com/bubblesort-ex1
Traditional Version
http://tobiasstraub.com/bubblesort-ex2
tobiasstraub.com
About the Author
Tobias Straub is a Web and Mobile
Developer from Germany. Write an email to
talk with him or follow him on LinkedIn.
tobiasstraub.com/linkedin
hello@tobiasstraub.com
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0
Unported License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-nd/3.0/.
Image sources
Page 1: Keith, http://www.flickr.com/photos/outofideas/
License

Contenu connexe

Tendances

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 

Tendances (20)

3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Bubble sort algorithm
Bubble sort algorithmBubble sort algorithm
Bubble sort algorithm
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Quick sort
Quick sortQuick sort
Quick sort
 
Sorting
SortingSorting
Sorting
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 

En vedette

Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
 
Selection sort
Selection sortSelection sort
Selection sort
asra khan
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 

En vedette (20)

Bubble sort
Bubble sort Bubble sort
Bubble sort
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Mergesort
MergesortMergesort
Mergesort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Sorting
SortingSorting
Sorting
 
Selection sort
Selection sortSelection sort
Selection sort
 
The selection sort algorithm
The selection sort algorithmThe selection sort algorithm
The selection sort algorithm
 
Sorting
SortingSorting
Sorting
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Python Day1
Python Day1Python Day1
Python Day1
 

Similaire à Bubblesort Algorithm

Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Number system....
Number system....Number system....
Number system....
mshoaib15
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
andreecapon
 
Decimals Add Subtract
Decimals Add SubtractDecimals Add Subtract
Decimals Add Subtract
hiratufail
 

Similaire à Bubblesort Algorithm (20)

Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
Contéo de figuras
Contéo de figurasContéo de figuras
Contéo de figuras
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlab
 
MFCS UNIT-III.pptx
MFCS UNIT-III.pptxMFCS UNIT-III.pptx
MFCS UNIT-III.pptx
 
chapter2.pptx electrical engineering for student
chapter2.pptx electrical engineering for studentchapter2.pptx electrical engineering for student
chapter2.pptx electrical engineering for student
 
Sorting
SortingSorting
Sorting
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Matrix introduction
Matrix introduction Matrix introduction
Matrix introduction
 
Matrix introduction and matrix operations.
Matrix introduction and matrix operations. Matrix introduction and matrix operations.
Matrix introduction and matrix operations.
 
Number systems and conversions
Number systems and conversionsNumber systems and conversions
Number systems and conversions
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Number System and Boolean Algebra
Number System and Boolean AlgebraNumber System and Boolean Algebra
Number System and Boolean Algebra
 
Number system....
Number system....Number system....
Number system....
 
04 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa1604 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa16
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting algorithm
Sorting algorithmSorting algorithm
Sorting algorithm
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
 
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
Swifter Taipei 聊聊 Swift 遊樂場、變數常數、數字與運算
 
Decimals Add Subtract
Decimals Add SubtractDecimals Add Subtract
Decimals Add Subtract
 

Dernier

Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 

Dernier (20)

Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 

Bubblesort Algorithm