A well-known algorithm
called bubble sort proceeds by scanning the list from left to right and
whenever a pair of adjacent keys found out an order, then those items are swapped.
This process repeats till all the elements of the list are in sorted order.
Bubble Sort Program in C
Solution In C
Code:
//Bubble sort in C
//Bubble sort in C
#include<stdio.h>
int main()
{
int size;
printf("Enter the size of the array:");
scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
int out, in;
for(out=size-1; out>1; out--)
{
for(in=0; in<out; in++)
{
if( arr[in] > arr[in+1])
{
int temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
} }}
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
Input/Output:int main()
{
int size;
printf("Enter the size of the array:");
scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
int out, in;
for(out=size-1; out>1; out--)
{
for(in=0; in<out; in++)
{
if( arr[in] > arr[in+1])
{
int temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
} }}
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
Enter the size of the array:5
Enter the element of the array:
4
3
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9
Bubble Sort Program in C plus plus
Solution In C++
Code:
#include<iostream>
using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array:";
cin>>size;
int arr[size],i;
cout<<"Enter the element of the array:";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
int out, in;
for(out=size-1; out>1; out--)
{
for(in=0; in<out; in++)
{
if( arr[in] > arr[in+1])
{
int temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
} }}
cout<<"\nAfter Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
}
using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array:";
cin>>size;
int arr[size],i;
cout<<"Enter the element of the array:";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
int out, in;
for(out=size-1; out>1; out--)
{
for(in=0; in<out; in++)
{
if( arr[in] > arr[in+1])
{
int temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
} }}
cout<<"\nAfter Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
}
Input/Output:
Enter the size of the array:4
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9
Bubble Sort Program in Java
Code:
import java.util.Scanner;
public class Bubble_Sort {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
System.out.print("Enter the size of the array:");
size=cs.nextInt();
int arr[]=new int[size],i;
System.out.print("Enter the element of the array:");
for(i=0;i<size;i++)
arr[i]=cs.nextInt();
System.out.print("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
int out, in;
for(out=size-1; out>1; out--)
{
for(in=0; in<out; in++)
{
if( arr[in] > arr[in+1])
{
int temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
} }}
System.out.print("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
public class Bubble_Sort {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
System.out.print("Enter the size of the array:");
size=cs.nextInt();
int arr[]=new int[size],i;
System.out.print("Enter the element of the array:");
for(i=0;i<size;i++)
arr[i]=cs.nextInt();
System.out.print("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
int out, in;
for(out=size-1; out>1; out--)
{
for(in=0; in<out; in++)
{
if( arr[in] > arr[in+1])
{
int temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
} }}
System.out.print("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
Input/Output:
Enter the size of the array:4
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6
After Sorting Array Elements are: 2 2 3 6
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6
After Sorting Array Elements are: 2 2 3 6
Bubble Sort Program in python using list
Solution In Python
size=int(input("Enter the size of the array:"));
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before Sorting Array Element are: ",arr)
for out in range(size-1,0,-1):
for inn in range(out):
if arr[inn] > arr[inn +1]:
temp=arr[inn]
arr[inn]=arr[inn +1]
arr[inn +1]=temp
print("\nAfter Sorting Array Element are: ",arr)
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before Sorting Array Element are: ",arr)
for out in range(size-1,0,-1):
for inn in range(out):
if arr[inn] > arr[inn +1]:
temp=arr[inn]
arr[inn]=arr[inn +1]
arr[inn +1]=temp
print("\nAfter Sorting Array Element are: ",arr)
Enter the size of the array:5
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are: [4, 3, 6, 7, 4]
After Sorting Array Element are: [3, 4, 4, 6, 7]
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are: [4, 3, 6, 7, 4]
After Sorting Array Element are: [3, 4, 4, 6, 7]
0 Comments
Please do not Enter any spam link in the comment box