In this article, we will discuss the implementation of Bidirectional Bubble Sort
Bidirectional Bubble sort Program in C
Solution In C
Code:
#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 low = 0, high= size-1,temp,in;
while (low < high)
{
for ( in = low; in < high; in++)
{
if (arr[in] > arr[in+1]){
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}
}
high--;
for ( in = high; in > low; in--)
{
if (arr[in] < arr[in-1]){
temp=arr[in];
arr[in]=arr[in-1];
arr[in-1]=temp;}
}
low++;
}
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
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 low = 0, high= size-1,temp,in;
while (low < high)
{
for ( in = low; in < high; in++)
{
if (arr[in] > arr[in+1]){
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}
}
high--;
for ( in = high; in > low; in--)
{
if (arr[in] < arr[in-1]){
temp=arr[in];
arr[in]=arr[in-1];
arr[in-1]=temp;}
}
low++;
}
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
Input/Output:
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
Bidirectional 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 low = 0, high= size-1,temp,in;
while (low < high)
{
for ( in = low; in < high; in++)
{
if (arr[in] > arr[in+1]){
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}
}
high--;
for ( in = high; in > low; in--)
{
if (arr[in] < arr[in-1]){
temp=arr[in];
arr[in]=arr[in-1];
arr[in-1]=temp;}
}
low++;
}
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 low = 0, high= size-1,temp,in;
while (low < high)
{
for ( in = low; in < high; in++)
{
if (arr[in] > arr[in+1]){
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}
}
high--;
for ( in = high; in > low; in--)
{
if (arr[in] < arr[in-1]){
temp=arr[in];
arr[in]=arr[in-1];
arr[in-1]=temp;}
}
low++;
}
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
Bidirectional Bubble sort Program in Java
Code:
import java.util.Scanner;
public class Bidirectional_BubbleSort {
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 low = 0, high= size-1,temp,in;
while (low < high)
{
for ( in = low; in < high; in++)
{
if (arr[in] > arr[in+1]){
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}
}
high--;
for ( in = high; in > low; in--)
{
if (arr[in] < arr[in-1]){
temp=arr[in];
arr[in]=arr[in-1];
arr[in-1]=temp;}
}
low++;
}
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
Bidirectional 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)
low = 0
high= size-1
while low < high:
for inn in range(low, high):
if arr[inn] > arr[inn+1]:
temp=arr[inn]
arr[inn]=arr[inn+1]
arr[inn+1]=temp
high-=1
for inn in range(high,low,-1):
if arr[inn] < arr[inn-1]:
temp=arr[inn]
arr[inn]=arr[inn-1]
arr[inn-1]=temp
low+=1
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)
low = 0
high= size-1
while low < high:
for inn in range(low, high):
if arr[inn] > arr[inn+1]:
temp=arr[inn]
arr[inn]=arr[inn+1]
arr[inn+1]=temp
high-=1
for inn in range(high,low,-1):
if arr[inn] < arr[inn-1]:
temp=arr[inn]
arr[inn]=arr[inn-1]
arr[inn-1]=temp
low+=1
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