← All resources

Write a program to input an Array of size N and sort it using Bubble Sort Technique

import java.io.*;
public class array_bubble
{
    public static void main()throws IOException
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int a[], i, j, k, tmp, n;
        System.out.println("Enter the size of the array:");
        n = Integer.parseInt(r.readLine());
        a = new int[10];
        System.out.println("Enter the values of the array:");
        for(i=0;i < n;i++)
        {
            a[i] = Integer.parseInt(r.readLine());
        }
        //sorting the array
        for(i=0;i < n;i++)
        {
            for(j=0;j < n-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    tmp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = tmp;
                }
            }
        }
        //printing the array
        System.out.println("The sorted array:");
        for(i=0;i < n;i++)
        {
            System.out.println(a[i]);
        }
    }
}