public class  array{
  public static void main(String[] args){
    int num[] = {80,20,47,82,25,13};
    int l = num.length;
    int i,j;
    System.out.print("Given number : ");
    for (i = 0;i < l;i++ ){
      System.out.print("  " + num[i]);
    }
    System.out.println("\n");
    System.out.print("Accending order number : ");
    for (i = 0;i < l;i++ ){
    	//int temp=num[i];
    	 for (j = 0;j < l;j++ ){
    	if(num[i]>num[j])
    	{
    		num[i]=num[j];
    		
    	}
    }
    }
    for (i = 0;i < l;i++ ){
      System.out.print("  " + num[i]);
    }
    }
  }

output is coming wrong....

Given number : 80 20 47 82 25 13

Accending order number : 13 13 13 13 13 13

Recommended Answers

All 3 Replies

#
for (i = 0;i < l;i++ ){
#
//int temp=num[i];
#
for (j = 0;j < l;j++ ){
#
if(num[i]>num[j])
#
{
#
//num[i]=num[j];//wrong

int temp=num[j];
 num[j]=num[i];
  num[i]=temp;
#
 
#
}
#
}
#
}

or else simply use...

Arrays.sort(num);
commented: very prompt... +1

I like to use an open-source implementation of multi-core sorting algorithm from happy-commons

Integer[] a = ...;
//sort the array
Arrays_1x0.sort(a);

:-)
Andrej

for (i = 0;i< l;i++ )
    {
    	int temp=num[i];
    	 for (j = i;j<l;j++ )
    	 {
    		 if(num[i]>num[j])
    		 	{   			 
    			 	num[i]=num[j];
    			 	num[j]=temp;
    		   	}
    	 }
    }

Try this code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.