SlideShare una empresa de Scribd logo
1 de 7
Bubble Sort
Learn How bubble sort work, it’s usage and best
practices By: Gaurav Kumar
High-Level Thoughts
Runtime Complexity:
* Best Case : O(n)
* Average Case : O(n^2)
* Worst Case : O(n^2)
Memory Space: O(1),No Additional Space required
Stability: Stable, does not change the relative order of elements with equal keys
Adaptive: O(n) when nearly sorted
Idea for Usage
This Sorting Algorithm is suitable when we have small data sets because
bubble sort is very slow for large data sets as running complexity is O(n^2).
Bubble sort is stable sorting and takes no extra space so these are advantages
of this algorithm.
Bubble Sort Concept
In the bubble sort, as elements are sorted they gradually "bubble" (or rise) to
their proper location in the array, like bubbles rising in a glass of soda. Instead
of searching an array as a whole, the bubble sort works by comparing adjacent
pairs of objects in the array. If the objects are not in the correct ordered, they
are swapped so that the largest of the two moves up. This process continues
until the largest of the objects, eventually "bubbles" up to the highest position in
the array. After this occurs, the search for the next largest object begins. The
swapping continues until the whole array is in the correct order.
Code Snippet private static int[] bubbleSort(int[] arr)
{
int alen = arr.length;
for (int i = 0; i < alen - 1; i++)
{
for (int j = 0; j < alen - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
How to Improve above code snippet?
As you see, there are two for loop are running and having running time
complexity O(n^2).But suppose input array is already sorted that time also
above code run O(n^2). Just think for a minute !
We have to write some logic in above code so that we could aware that input
array is already sorted. In bubble sort we are comparing each element with the
adjacent element if they are out of order then we are swapping. Then if our
input array is already sorted then there will be no swapping of elements. So we
could maintain a flag, if in first pass of for loop there is no swapping of element
then make that flag true and break the for loop for further operation.
Thanks!

Más contenido relacionado

La actualidad más candente

Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sortgeeortiz
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using CAshish Gaurkhede
 
Selection sort
Selection sortSelection sort
Selection sortamna izzat
 
Selection sort
Selection sortSelection sort
Selection sortstella D
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting TechniquesRafay Farooq
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1asmhemu
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMARDOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMARPrasunKumar38
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 

La actualidad más candente (20)

Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Selection sort
Selection sortSelection sort
Selection sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Selection sort
Selection sortSelection sort
Selection sort
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMARDOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 

Similar a How bubble sort works

MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxArjayBalberan1
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048vital vital
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Sorting Algorithms.
Sorting Algorithms.Sorting Algorithms.
Sorting Algorithms.Saket Kumar
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080Aravind NC
 

Similar a How bubble sort works (20)

MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting Algorithms.
Sorting Algorithms.Sorting Algorithms.
Sorting Algorithms.
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
SHELL SORT-2.pptx
SHELL SORT-2.pptxSHELL SORT-2.pptx
SHELL SORT-2.pptx
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
Selection sort
Selection sortSelection sort
Selection sort
 
L1803016468
L1803016468L1803016468
L1803016468
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
Sorting
SortingSorting
Sorting
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

How bubble sort works

  • 1. Bubble Sort Learn How bubble sort work, it’s usage and best practices By: Gaurav Kumar
  • 2. High-Level Thoughts Runtime Complexity: * Best Case : O(n) * Average Case : O(n^2) * Worst Case : O(n^2) Memory Space: O(1),No Additional Space required Stability: Stable, does not change the relative order of elements with equal keys Adaptive: O(n) when nearly sorted
  • 3. Idea for Usage This Sorting Algorithm is suitable when we have small data sets because bubble sort is very slow for large data sets as running complexity is O(n^2). Bubble sort is stable sorting and takes no extra space so these are advantages of this algorithm.
  • 4. Bubble Sort Concept In the bubble sort, as elements are sorted they gradually "bubble" (or rise) to their proper location in the array, like bubbles rising in a glass of soda. Instead of searching an array as a whole, the bubble sort works by comparing adjacent pairs of objects in the array. If the objects are not in the correct ordered, they are swapped so that the largest of the two moves up. This process continues until the largest of the objects, eventually "bubbles" up to the highest position in the array. After this occurs, the search for the next largest object begins. The swapping continues until the whole array is in the correct order.
  • 5. Code Snippet private static int[] bubbleSort(int[] arr) { int alen = arr.length; for (int i = 0; i < alen - 1; i++) { for (int j = 0; j < alen - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; }
  • 6. How to Improve above code snippet? As you see, there are two for loop are running and having running time complexity O(n^2).But suppose input array is already sorted that time also above code run O(n^2). Just think for a minute ! We have to write some logic in above code so that we could aware that input array is already sorted. In bubble sort we are comparing each element with the adjacent element if they are out of order then we are swapping. Then if our input array is already sorted then there will be no swapping of elements. So we could maintain a flag, if in first pass of for loop there is no swapping of element then make that flag true and break the for loop for further operation.