SlideShare a Scribd company logo
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
Exchange/Bubble Sort:
It uses simple algorithm. It sorts by comparing each pair
of adjacent items and swapping them in the order. This
will be repeated until no swaps are needed. The
algorithm got its name from the way smaller elements
"bubble" to the top of the list.
It is not that much efficient, when a list is having more
than a few elements. Among simple sorting algorithms,
algorithms like insertion sort are usually considered as
more efficient.
Bubble sort is little slower compared to other sorting
techniques but it is easy because it deals with only two
elements at a time.
"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
5
12
35
42
77 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 77
"Bubbling Up" the Largest Element
5
77
12
35
42 101
1 2 3 4 5 6
No need to swap
Bubble Sort
1. In each pass, we compare adjacent elements and swap them if they are
out of order until the end of the list. By doing so, the 1st pass ends up
“bubbling up” the largest element to the last position on the list
2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1
passes, the list is sorted.
Example:
Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17
45 89 | 68 90 29 34 17 45 68 | 89 29 34 17
68 89 | 90 29 34 17 68 89 | 29 34 17
89 90 | 29 34 17 29 89 | 34 17
29 90 | 34 17 34 89 | 17
34 90 | 17 17 89
17 90
45 68 89 29 34 17 90 45 68 29 34 17 89
largest 2nd largest
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
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
// Optimized java implementation of Bubble sort
import java.io.*;
class GFG {
// An optimized version of Bubble Sort
static void bubbleSort(int arr[], int n)
{
int i, j, temp;
boolean swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no two elements were
// swapped by inner loop, then break
if (swapped == false)
break;
}
}
// Function to print an array
static void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = arr.length;
bubbleSort(arr, n);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}
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
• We reduce the number of elements we
compare each time one is correctly placed
Thank You

More Related Content

PPT
Bubble sort
Manek Ar
 
PPTX
BUBBLESORT
Ashish Sadavarti
 
PPTX
Bubble Sort presentation.pptx
TusharTikia
 
PPTX
bubblesort-130417100323-phpapp02[1].pptx
TusharTikia
 
PPTX
Bubble Sort.pptx
MuhammadShafi89
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPT
Sorting algorithms
Zaid Hameed
 
PPTX
Bubble sort
Rashmi R Upadhya
 
Bubble sort
Manek Ar
 
BUBBLESORT
Ashish Sadavarti
 
Bubble Sort presentation.pptx
TusharTikia
 
bubblesort-130417100323-phpapp02[1].pptx
TusharTikia
 
Bubble Sort.pptx
MuhammadShafi89
 
Unit 7 sorting
Dabbal Singh Mahara
 
Sorting algorithms
Zaid Hameed
 
Bubble sort
Rashmi R Upadhya
 

Similar to MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx (20)

PPT
ds 3Sorting.ppt
AlliVinay1
 
PPT
Sorting
Sameer Memon
 
PPT
Sorting algos
Omair Imtiaz Ansari
 
PPTX
Searching and sorting Techniques in Data structures
PRIANKA R
 
PPT
Sorting algorithm
Muhammad Farhan
 
PPT
Sorting algorithms v01
Dusan Vuckovic
 
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
PPT
Sorting algorithms
Edward Blurock
 
PPTX
Presentation about Bubble Sort
Md. Masud Rana Ashik
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
PPTX
8 elementary sorts-bubble
irdginfo
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PPT
Data Structure and Algorithms Sorting
ManishPrajapati78
 
PPT
3.1 bubble sort
Krish_ver2
 
PPT
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
m.kumarasamy college of engineering
 
PPT
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
PPTX
SORTING techniques.pptx
Dr.Shweta
 
PPTX
Chapter #4 (Bubble Sort jfdkjffdfdf).pptx
hekmatyarzahir44
 
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
ds 3Sorting.ppt
AlliVinay1
 
Sorting
Sameer Memon
 
Sorting algos
Omair Imtiaz Ansari
 
Searching and sorting Techniques in Data structures
PRIANKA R
 
Sorting algorithm
Muhammad Farhan
 
Sorting algorithms v01
Dusan Vuckovic
 
Data Structures - Lecture 8 [Sorting Algorithms]
Muhammad Hammad Waseem
 
Sorting algorithms
Edward Blurock
 
Presentation about Bubble Sort
Md. Masud Rana Ashik
 
Unit vii sorting
Tribhuvan University
 
Sorting Data structure And Algorithm.pptx
subhanalichand514
 
8 elementary sorts-bubble
irdginfo
 
L 14-ct1120
Zia Ush Shamszaman
 
Data Structure and Algorithms Sorting
ManishPrajapati78
 
3.1 bubble sort
Krish_ver2
 
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
m.kumarasamy college of engineering
 
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
SORTING techniques.pptx
Dr.Shweta
 
Chapter #4 (Bubble Sort jfdkjffdfdf).pptx
hekmatyarzahir44
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
Ad

More from ArjayBalberan1 (20)

PDF
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
ArjayBalberan1
 
PDF
PPE-Module-1.1 PPE-Module-1.1 PPE-Module-1.1.pdf
ArjayBalberan1
 
PPTX
LESSON 5 LESSON 5 LESSON 5 LESSON 5.pptx
ArjayBalberan1
 
PPTX
LESSON 1 AUTOMATA LESSON 1 AUTOMATA.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA Automata10Chapter-8-and-9.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA AUTOMATAAutomata9Chapter8.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA Automata8Chapter7.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA AUTOMATAAutomata7Chapter6.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA AUTOMATA Automata6Chapter5.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA Automata5Chapter4.pptx
ArjayBalberan1
 
PPTX
AUTOMATA AUTOMATA Automata4Chapter3.pptx
ArjayBalberan1
 
PPTX
AUTOMATA THEORY AUTOMATA THEORYAutomata3Chapter2.pptx
ArjayBalberan1
 
PPTX
automata theory and formal languages Automata2Chapter1.pptx
ArjayBalberan1
 
PPTX
Group-1 (1) Group-1 (1) Group-1 (1)Group-1 (1).pptx
ArjayBalberan1
 
PPTX
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
ArjayBalberan1
 
PPTX
Introduction to Algorithms Introduction to Algorithms.pptx
ArjayBalberan1
 
PPTX
MYSQL DATABASE MYSQL DATABASEGroup-1.pptx
ArjayBalberan1
 
PPTX
Appdev appdev appdev app devAPPDEV 1.2.pptx
ArjayBalberan1
 
PPTX
Rizals-Family-Childhood-Early-Education.pptx
ArjayBalberan1
 
PPTX
MYSQL DATABASE Operating System Part2 (1).pptx
ArjayBalberan1
 
PPE-Module-1.2 PPE-Module-1.2 PPE-Module-1.2.pdf
ArjayBalberan1
 
PPE-Module-1.1 PPE-Module-1.1 PPE-Module-1.1.pdf
ArjayBalberan1
 
LESSON 5 LESSON 5 LESSON 5 LESSON 5.pptx
ArjayBalberan1
 
LESSON 1 AUTOMATA LESSON 1 AUTOMATA.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA Automata10Chapter-8-and-9.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA AUTOMATAAutomata9Chapter8.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA Automata8Chapter7.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA AUTOMATAAutomata7Chapter6.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA AUTOMATA Automata6Chapter5.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA Automata5Chapter4.pptx
ArjayBalberan1
 
AUTOMATA AUTOMATA Automata4Chapter3.pptx
ArjayBalberan1
 
AUTOMATA THEORY AUTOMATA THEORYAutomata3Chapter2.pptx
ArjayBalberan1
 
automata theory and formal languages Automata2Chapter1.pptx
ArjayBalberan1
 
Group-1 (1) Group-1 (1) Group-1 (1)Group-1 (1).pptx
ArjayBalberan1
 
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
ArjayBalberan1
 
Introduction to Algorithms Introduction to Algorithms.pptx
ArjayBalberan1
 
MYSQL DATABASE MYSQL DATABASEGroup-1.pptx
ArjayBalberan1
 
Appdev appdev appdev app devAPPDEV 1.2.pptx
ArjayBalberan1
 
Rizals-Family-Childhood-Early-Education.pptx
ArjayBalberan1
 
MYSQL DATABASE Operating System Part2 (1).pptx
ArjayBalberan1
 
Ad

Recently uploaded (20)

PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Doc9.....................................
SofiaCollazos
 
This slide provides an overview Technology
mineshkharadi333
 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 

MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx

  • 2. Sorting • Sorting takes an unordered collection and makes it an ordered one. 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 3. Exchange/Bubble Sort: It uses simple algorithm. It sorts by comparing each pair of adjacent items and swapping them in the order. This will be repeated until no swaps are needed. The algorithm got its name from the way smaller elements "bubble" to the top of the list. It is not that much efficient, when a list is having more than a few elements. Among simple sorting algorithms, algorithms like insertion sort are usually considered as more efficient. Bubble sort is little slower compared to other sorting techniques but it is easy because it deals with only two elements at a time.
  • 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 5 12 35 42 77 101 1 2 3 4 5 6
  • 5. "Bubbling Up" the Largest Element 5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77
  • 6. "Bubbling Up" the Largest Element 5 77 12 35 42 101 1 2 3 4 5 6 No need to swap
  • 7. Bubble Sort 1. In each pass, we compare adjacent elements and swap them if they are out of order until the end of the list. By doing so, the 1st pass ends up “bubbling up” the largest element to the last position on the list 2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1 passes, the list is sorted. Example: Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17 45 89 | 68 90 29 34 17 45 68 | 89 29 34 17 68 89 | 90 29 34 17 68 89 | 29 34 17 89 90 | 29 34 17 29 89 | 34 17 29 90 | 34 17 34 89 | 17 34 90 | 17 17 89 17 90 45 68 89 29 34 17 90 45 68 29 34 17 89 largest 2nd largest
  • 8. 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 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 9. // Optimized java implementation of Bubble sort import java.io.*; class GFG { // An optimized version of Bubble Sort static void bubbleSort(int arr[], int n) { int i, j, temp; boolean swapped; for (i = 0; i < n - 1; i++) { swapped = false; for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { // Swap arr[j] and arr[j+1] temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = true; } } // If no two elements were // swapped by inner loop, then break if (swapped == false) break; } } // Function to print an array static void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) System.out.print(arr[i] + " "); System.out.println(); } // Driver program public static void main(String args[]) { int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; int n = arr.length; bubbleSort(arr, n); System.out.println("Sorted array: "); printArray(arr, n); } }
  • 10. 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 • We reduce the number of elements we compare each time one is correctly placed