Print An Array, In Ascending Order In Java (Click On Full View ↑ )

Sort The Elements Of An array In Ascending Order. (In Java)

 ---------------------------------------------------------------------------------------------------------------------------- 

public class SortArray
{
    public static void main(String[] args)
{
          int[] arr= {5,4,3,2,1};
          int temp=0;

 for(int i = 0; i < arr.length; i++)
  {  
    for(int j = i+1; j < arr.length; j++)
    {  
       if(arr[i] > arr[j])
       { 
           temp = arr[i]; 
           arr[i] = arr[j]; 
           arr[j] = temp; 
       }  
    }  
  } 

      for (int i = 0; i < arr.length; i++)
      {  
          System.out.print(arr[i]+" "); 
      } 
  }

   

 }
 ----------------------------------------------------------------------------------------------------------------------------

Output :

1 2 3 4 5 

 

 

 

 

 

 

 

 


Comments

Popular posts from this blog

Mr. Myers wants to edit the marks assigned to the questions

Genral Ability

Object Orinted Programing