import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args) throws IOException, IndexOutOfBoundsException
{
int num;
System.out.println ("bubble sort");
System.out.println ("enter number of elements");
Scanner input = new Scanner(System.in);
num = input.nextInt();
BubbleImpl impl = new BubbleImpl(num);
System.out.println("enter element to sort");
for (int i = 0; i < num; i++)
impl.A[i] = input.nextInt();
impl.bubblesort(num);
impl.display(num);
}
}
class BubbleImpl
{
public int[] A;
public BubbleImpl (int n)
{
A = new int[n];
}
void bubblesort (int n)
{
boolean sorted = true;
int temp;
int swap = 0;
for (int j = 0; j <= n-2; j++)
{
if(A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
swap++;
sorted = false;
}
}
System.out.println("no. of interchanges are " + swap);
if(!sorted)
bubblesort(n);
}
void display(int n)
{
System.out.println("\nSorted list is");
for (int i = 0; i < n; i++)
System.out.println("position " + i + ", element: " + A[i]);
}
}