Turn Desktop View Off
You are here:   HomeJava ProgrammingJava Array
by Dinesh Thakur Category: array

Algorithm for SortinganArray:

step 1: set temp=0

step 2: read arr_size

step 3: create an integer array a[] of size arr_size

step 4: initialize i=0

step 5: repeat through step-11 while i less than a.length

step 6: read a[i]

step 7: initialize j=0

step 8: repeat through step-l0 while j less than a.length

step 9: if a[i] less than a[j] then interchange value of a[i] and a[j]

step 10: j=j+1

step 11: initialize i=0

step 12: repeat through step-14 while i less than a.length

step 13: print a[i]

step 14: i=i+ 1

step 15:Exit

Here is the Java Example for Sorting an Array:

import java.util.Scanner;

public class SortinganArray
{
               public static void main(String args[])
         {
               int i,j,temp=0;
               Scanner s=new Scanner(System. in) ;
               System.out.print("Enter The Size of The Array : ");
               int size=s.nextInt();
               int a[]=new int[size];
               System.out.println("Enter The array Elements");
               for(i=0;i<a.length;i++)
                  {
                     a[i]=s.nextInt();
                       for(j=0;j<a.length;j++)
                          {
                              if(a[i]<a[j])
                                {
                                    temp=a[j];
                                    a[j]=a[i] ;
                                    a[i]=temp;
                                 }
                           }
                  }
System.out.println("\nSorted an Array in Ascending Order \n");
                for(i=0;i<a.length;i++)
                   {
                      System.out.print(a[i]+"\t");
                    }
          }
}

Sorting an Array in Ascending Order





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article