Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Sunday 30 June 2013

Java program to sort a given array

Java program to sort a given array.

Code :
class Array{
    int a[]={9,7,4,-1,0};
    voidsortArray(){
    int i,j,temp;
    for(i=0;i<a.length;i++){
        for(j=0;j<a.length-1;j++){
            if(a[j]>a[j+1]){
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }
    voidshowArray(){
        for(inti=0;i<a.length;i++){
            System.out.print(" a["+i+"] : "+a[i]+"\t");
        }
    }
}
class Sort{
    public static void main(String args[]){
        Array t=new Array();
        System.out.print("\n Array Elements : \n ");
        t.showArray();
        System.out.print("\n Sorted Array Elements : \n ");
        t.sortArray();
        t.showArray();
    }
}
Output :
Array Elements :
  a[0] : 9       a[1] : 7        a[2] : 4        a[3] : -1       a[4] : 0

 Sorted Array Elements :
  a[0] : -1      a[1] : 0        a[2] : 4        a[3] : 7        a[4] : 9
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

No comments:

Post a Comment