Bubble Sorting Alorithm Data Structure.ppt

rasiso4714 0 views 12 slides Sep 14, 2025
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Bubble Sorting Algorithm


Slide Content

Bubble Sort
By Atharva Deorukhkar
18017

Algorithm
(Bubble Sort) BUBBLE(LA, N)
Here LA is an array with N elements.
This algorithm sorts the elements in LA.
1. Repeat Steps 2 and 3 for K = 1 to N - 1.
2. Set PTR := 1. [Initializes pass pointer PTR.]
3. Repeat while PTR ≤ N - K: [Executes pass.]
(a) If LA[PTR] < LA[PTR + 1], then:
Interchange LA[PTR] and LA[PTR + 1]. [End of If structure.]
(b) Set PTR := PTR + 1.
[End of inner loop.]
[End of Step 1 outer loop.]
4. Exit.

Working functionality
Pass KNPTR N-KA[PTR]=A[PTR+1] Temp A1 A2 A3 A4 A5
1 151 4 3>5 0 3 5 2 7 1
1 152 4 5>2 5 3 2 5 7 1
1 153 4 5>7 5 3 2 5 7 1
1 154 4 7>1 7 3 2 5 1 7
Let the array be [3,5,2,7,1]

[3,5,2,7,1]
pass k n ptr n-k a[ptr]=a[ptr+1] temp a1 a2 a3a4a5
2 2 5 1 3 3>2 3 2 2 5 1 7
2 2 5 2 3 3>5 3 2 3 5 1 7
2 2 5 3 3 5>1 5 2 3 1 5 7
3 3 5 1 2 2>3 5 2 3 1 5 7
3 3 5 2 2 3>1 3 2 1 3 5 7
4 4 5 1 1 2>1 2 1 2 3 5 7

lets see this how it works
lets take a array =[9,5,3,1,8,2]
9 5 3 1 8 2
5 9 3 1 8 2
5 3 9 1 8 2
5 3 1 9 8 2

5 3 1 8 9 2
5 3 1 8 2 9
3 5 1 8 2 9
3 1 5 8 2 9
3 1 5 8 2 9
3 1 5 2 8 9

1 3 5 2 8 9
1 3 5 2 8 9
1 3 2 5 8 9
1 3 2 5 8 9
1 2 3 5 8 9
1 2 3 5 8 9
Sorted Array

Code in C++
//implementing bubble sort
#include <iostream>
#include<conio.h>
void get(int i,int n,int *la){
for(i=0;i<=n;i++){
cout<<"enter the "<<i<<" element :";
cin>>la[i];
}
}

void bubble(int n,int *la){
int pass,temp,ptr;
for(pass=0;pass<n;pass++){
for(ptr=0;ptr<n;ptr++){
if(la[ptr]>la[ptr+1]){
temp=la[ptr];
la[ptr]=la[ptr+1];
la[ptr+1]=temp;
}
}
}
}

void display(int i,int n,int *la){
for(i=0;i<=n;i++){
cout<<la[i]<<endl;
}
}
void main(){
int n,i,j,la[100];
cout<<"enter the value your want insert:";
cin>>n;
get(i,n,la);
bubble(n,la);
display(i,n,la);
}

reference
data structures with c (schaum's outline series)

project
•i have made a project on bubble sorting
•here is the link
https://scgbgrf7kzep0zua15uplq.on.drv.tw/
projectbubblesort954.blog/