• Post author:
  • Reading time:4 mins read
  • Post category:C Programs

If you are searching for Bubble sort in c | c++ and java you are at the right article.

Table of Contents

Serial NumberTopic
1What is bubble sort?
2Programming Pre-requisites
3Bubble sort Algorithm For Ascending Sort
4Bubble sort Algorithm For Descending Sort
5Bubble sort in c
6Bubble sort in c++
7Bubble sort in Java
8Programming Suggestions

What is bubble sort?

Bubble sort is an inplace sorting algorithm. It iterates array in two level nested for loops from zero to n-1 where n is size of array. Element indexed by outer for loop is being compared with every other element of that array. If that element is smaller than the element indexed by outer for loop then both elements are swapped this is incase of ascending sorting. If you want to sort an array in descending order in bubble sort then swap elements if element indexed by outer loop element is greater than the element indexed by inner loop element then perform swap operation.


Programming Pre-requisites

  • Data-typea in c | c++ and java.
  • for loop in c | c++ and java.
  • Swapping concept.
  • Relational operator in c | c++ and java.

Bubble sort Algorithm For Ascending Sort

  • Step 1: Initialize array elements
  • Step 2 : For i <- 0 to n-1
    For j <- 0 to n-1
    If Array [ i ] < Array [ j ]
    Swap Array [ i ] and Array [ j ]

Bubble sort Algorithm For Descending Sort

  • Step 1: Initialize array elements
  • Step 2 : For i <- 0 to n-1
    For j <- 0 to n-1
    If Array [ i ] < Array [ j ]
    Swap Array [ i ] and Array [ j ]

Bubble sort in c

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("\nBubble sort in c");
	printf( "\nEnter number of array elements\n" );
	int n;
	scanf("%d",&n);
	int array[n];
	printf( "\nEnter array elements\n" );
	for( int i=0 ; i<n ; i++ )
	{
		scanf("%d",&array[i]);
	}
	//Lets sort array using bubble sort
	for( int i=0 ; i<n ; i++ ) 
	{
		for( int j=0 ; j<n ; j++ )
		{
			if( array[i]<array[j] )
			{
				int temp=array[i];
				array[i]=array[j];
				array[j]=temp;
			}
		}
	}
	printf("\nArray after sorting through bubble sort:\n");
	for( int i=0 ; i<n ; i++ )
	{
	  printf("\t%d",array[i]);
	}
	printf("\n");
	return 0;
}
Bubble sort in c
Bubble sort in c

Bubble sort in c++

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
	cout<<"Bubble sort in c++"<<endl;
	cout<<"Enter number of array elements"<<endl;
	int n;
	cin>>n;
	int array[n];
	cout<<"Enter array elements"<<endl;
	for( int i=0 ; i<n ; i++ )
	{
		cin>>array[i];
	}
	for( int i=0 ; i<n ; i++ ) 
	{
		for( int j=0 ; j<n ; j++ )
		{
			if( array[i]<array[j] )
			{
				int temp=array[i];
				array[i]=array[j];
				array[j]=temp;
			}
		}
	}
	cout<<"Array after sorting through bubble sort:"<<endl;
	for( int i=0 ; i<n ; i++ )
	{
	  cout<<array[i]<<"\t"<<endl;
	}
	return 0;
}
Bubble sort in c++
Bubble sort in c++

Bubble sort in Java

import java.util.Scanner;

public class BubbleSortInJava {

	public static void main(String[] args) {
		System.out.println("Bubble Sort in Java");
		int n;
		System.out.println("Enter the number of elements");
		Scanner stdin=new Scanner(System.in);
		n=stdin.nextInt();
		int[] array=new int[n];
		System.out.println("Enter the elements");
		for( int i=0 ; i<n ; i++ )
		{
			array[i]=stdin.nextInt();
		}
		
		for( int i=0 ; i<n ; i++ )
		{
			for( int j=0 ; j<n; j++ )
			{
				if( array[i]<array[j] )
				{
					int temp=array[i];
					array[i]=array[j];
					array[j]=temp;
				}
			}
		}
		System.out.println("Array after sorting using Bubble sort");
		for( int i=0 ; i<n ; i++ )
		{
			System.out.print(array[i]+"\t");
		}
		

	}

}
Bubble sort in java
Bubble sort in java

Suggested further practice