BUBBLE SORT
Bubble Sort Is A Type Of Sorting In Which All The Elements Present In The Array/List Are Sorted By Comparing Elements With The Elements Adjacent To It. Bubble Sorting Is The Most Simplest Sorting In Terms Of Coding And Understandability.
COMPLEXITY :
BEST TIME COMPLEXITY = O(n^2)
AVERAGE TIME COMPLEXITY = θ(n^2)
WORST TIME COMPLEXITY = Ω(n^2)
BEST SPACE COMPLEXITY = Ω(1)
CODE IMPLEMENTATION :
C++
#include <iostream>
using namespace std;
int main() {
//Declaring An Array
int a[] = {2,4,1,0,2,5,3};
//Getting The No.Of Elements Present In The Array
int size = (sizeof(a)/sizeof(a[0]));
int n = size;
//Bubble Sort Main Function
for(int i=0; i<size; i++)
{
for(int j =0; j<n-1; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
n = n-1;
}
//Displaying The Sorted Array
for(int i = 0; i<size; i++)
{
cout<<a[i]<<endl;
}
}
using namespace std;
int main() {
//Declaring An Array
int a[] = {2,4,1,0,2,5,3};
//Getting The No.Of Elements Present In The Array
int size = (sizeof(a)/sizeof(a[0]));
int n = size;
//Bubble Sort Main Function
for(int i=0; i<size; i++)
{
for(int j =0; j<n-1; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
n = n-1;
}
//Displaying The Sorted Array
for(int i = 0; i<size; i++)
{
cout<<a[i]<<endl;
}
}
JAVA
public class MyClass {
public static void main(String args[]) {
//Declaring An Array
int a[] = {2,4,1,0,2,5,3};
//Getting Size Of The Array
int size = a.length;
int n = size;
//Bubble Sort Main Function
for(int i=0; i<size; i++)
{
for(int j=0; j<n-1; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
n = n-1;
}
//Displaying The Sorted Array
for(int i: a)
{
System.out.println(i);
}
}
}
public static void main(String args[]) {
//Declaring An Array
int a[] = {2,4,1,0,2,5,3};
//Getting Size Of The Array
int size = a.length;
int n = size;
//Bubble Sort Main Function
for(int i=0; i<size; i++)
{
for(int j=0; j<n-1; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
n = n-1;
}
//Displaying The Sorted Array
for(int i: a)
{
System.out.println(i);
}
}
}
PYTHON
#Declaring A List
a =[2,4,1,0,2,5,3]
#Getting No.Of Elements Present In The List
size = len(a)
n = size;
#Bubble Sort Main Function
for i in range(0,size):
for j in range(0,n-1):
if a[j]>a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
n = n-1
#Dislaying The Sorted List
for i in a:
print(i)
a =[2,4,1,0,2,5,3]
#Getting No.Of Elements Present In The List
size = len(a)
n = size;
#Bubble Sort Main Function
for i in range(0,size):
for j in range(0,n-1):
if a[j]>a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
n = n-1
#Dislaying The Sorted List
for i in a:
print(i)
Thanks for sharing this blog. This article gives lot of information.
ReplyDeletePython Training in Hyderabad
Python Training
Python Online Training