Monday, 15 January 2018

Sorting of an Array Example in java

package com.tutorial4u;

import java.util.Arrays;

public class ArraySort
{
     public static void main(String[] args)
     {
          int a [] = { 101,9,18,4,30,20};

          for (int i = 0; i<a.length; i++)
         {
             for (int j = 0; j<a.length; j++)
             {
                 if (a[i] < a[j])
                 {
                     int temp = a[i];
                     a[i] = a[j];
                     a[j] = temp;
                 }
             }
         }
          System.out.println("Sorted array is :"+Arrays.toString(a));
     }
}



Output:

Sorted array is :[4, 9, 18, 20, 30, 101]

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...