Bubble Sorting An Array | Write a program in java to sort an array by using bubble sort. | ISC Computer Science | ICSE Computer Applications

Bubble Sort in Java

Algorithm

  1.   Start
  2. a[]=null
  3. i:=0
  4. n:=0
  5. j:=0
  6. t:=0
  7. Print "Enter the size of the array"
  8. Read n
  9. a[]=new int[n]
  10. i=0
  11. If i<n, go to step 12 or else go to step 15
  12. Print "Enter a number"
  13. Read a[i]
  14. i++. Go to step 11
  15. i=0
  16. If i<n, go to step 17 or else go to step 25
  17. j=1
  18. If j<n-i, go to step 19 or else go to step 24
  19. If a[j-1].a[j], go to step 20 or else go to step 23
  20. t=a[j-1]
  21. a[j-1]=a[j]
  22. a[j]=t
  23. j++. Go to step 18
  24. i++. Go to step 16
  25. i=0
  26. i<n, go to step 27 or else go to step 29
  27. Print a[i]
  28. i++. Go to step 26
  29. Stop

Program

Buffered Reader

import java.io.*;
class SortArray
{
    int a[], i, n, j, t;
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
        System.out.println("Enter the size of the array");
        n=Integer.parseInt(br.readLine());
        a=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number");
            a[i]=Integer.parseInt(br.readLine());
        }
    }
    
    void sort()
    {
        for(i=0;i<n;i++)
        {
            for(j=1;j<n-i;j++)
            {
                if(a[j-1]>a[j])
                {
                    t=a[j-1];
                    a[j-1]=a[j];
                    a[j]=t;
                }
            }
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(a[i]);
        }
    }
    
    void main() throws IOException
    {
        accept();
        sort();
        display();
    }
}

Scanner

import java.util.*;
class SortArray
{
    int a[], i, n, j, t;
    
    void accept() 
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter the size of the array");
        n=sc.nextInt();
        a=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number");
            a[i]=sc.nextInt();
        }
    }
    
    void sort()
    {
        for(i=0;i<n;i++)
        {
            for(j=1;j<n-i;j++)
            {
                if(a[j-1]>a[j])
                {
                    t=a[j-1];
                    a[j-1]=a[j];
                    a[j]=t;
                }
            }
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(a[i]);
        }
    }
    
    void main() 
    {
        accept();
        sort();
        display();
    }
}

Documentation

Variable Description

Data Type Variable NameDescription
 int[]To store an array of numbers 
 intTo store the size of array 
 int To act as a counter variable 
 intTo act as a counter variable 
 intTo act as a temp variable 

Method Description

Return Type Method NameDescription
 voidaccept() To accept data from the user 
 voidsort() To sort the array 
 voiddisplay() To display an array 
 voidmain() To act as an entry gateway for the compiler 

Output


Output of Bubble Sort




Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences