Bubble Sort

import java.io.*;

public class Bubblesort
{
public static void main(String[] args)throws IOException
{
int[] a = new int[5]; // Declaring array of 5 elements
int i,j,c;
BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Numbers to sort :-");
for(i=0;i<5;i++)
{
a[i] = Integer.parseInt(br.readLine()); // Taking Inputs
}

for(i=0;i<5;i++) // Bubble Sort Logic
{
for(j=1;j<5-i;j++)
{
if(a[j-1] > a[j])
{
c = a[j-1];
a[j-1] = a[j];
a[j] = c;
}
}
}
System.out.println("The Sorted Elements Are :-");
for(i=0;i<5;i++)
{
System.out.print(a[i] + " "); // Printing The Sorted Elements
}
}
}

/*
Enter Numbers to sort :-
INPUT :
4
7
2
5
1
OUTPUT :
The Sorted Elements Are :-
1 2 4 5 7 
*/

Comments

Popular Posts