Sort array elements in ascending order/Bubble sorting algorithm in ascending order



CODE:
import java.util.*;
class Ascend
{
     public static void main(String args[])
     {
           int i,j,n,temp;
           Scanner sc = new Scanner(System.in);
           System.out.println("Enter number of elements:");
           n=sc.nextInt();
           int a[]=new int[n];
           for(i=0;i<=n-1;i++)
           {
                System.out.print("enter a no:");
                a[i]=sc.nextInt();
           }
           for(i=0;i<=n-2;i++)
           {
                for(j=0;j<=n-2;j++)
                {
                     if(a[j]>a[j+1])
                     {
                           temp=a[j];
                           a[j]=a[j+1];
                           a[j+1]=temp;
                     }
                }
           }
           System.out.println("After sorting:");
           for(i=0;i<=n-1;i++)
           {
                System.out.println(a[i]);
           }
     }
}

OUTPUT:
   



No comments :

Post a Comment