import java.io.*;
class Bubble
{
public static void main ( String args[] )
throws IOException
{
BufferedReader br = new BufferedReader ( new InputStreamReader ( System.in ) );
System.out.print("\n\nEnter size of array : ");
int n = Integer.parseInt ( br.readLine() );
int a[] = new int[n];
System.out.println("\n\nEnter array elements : \n");
for ( int i=0 ; i<n ; i++ )
{
System.out.print("\na[" + i + "] = ");
a[i] = Integer.parseInt ( br.readLine() );
}
for ( int i=0 ; i<n-1 ; i++ )
{
for ( int j=0 ; j<n-1-i ; j++ )
{
if ( a[j] > a[j+1] )
{
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
System.out.print("\n\nSorted Array : ");
for ( int i=0 ; i<n ; i++ )
System.out.print(a[i] + " ");
System.out.println("\n");
}
}
No comments:
Post a Comment