Tuesday, April 16, 2013

Array in Ascending order

      Array is a collection of homogeneous fixed size of elements.Sorting of array is a Frequently asked question in interview. Sort the Array by using Swap of two numbers Technic.

The following is Sorting of Array in ascending order:

import java. io.*;
import java.lang.*;
class ArraySort
{
  public static void main(String[] args) 
   {
      int a[]={5,2,1,3,8,9,6,4};
     ArraySort as=new ArraySort();
     as.sArray(a);
    }
  public void sArray(int[] a)
 {
   int b=a.length;
   for(int i=0;i<b;i++)
   {
      for(int j=0;j<i;j++)
      {
         if(a[i]<a[j])
         {
            int temp=a[i];
            a[i]=a[j];
            a[j]=temp;
          }
      }
    }
   for(int i=0;i<=b;i++)
   {
     System.out.println(a[i]);
   }
  }
}


Output:
1
2
3
4
5
6
8
9


No comments:

Post a Comment