array program to arrange in ascending order but not getting correct output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

array program to arrange in ascending order but not getting correct output

package book; public class Jfile{ public static void main(String[] args) { int a[] = {69,89,78,25,36,46}; // 343 int temp; for(int i=0;i<a.length;i++) { for(int j=0;j<a.length-1;j++) { if(a[i]<a[j+1])// 0 1 2 3 4 5 { temp=a[i]; a[i]=a[j+1]; a[j+1]=temp; } } } for(int i=0;i<a.length;i++) System.out.print(a[i] + " "); } }

9th May 2021, 12:26 PM
Sachin Saxena
Sachin Saxena - avatar
1 Answer
+ 2
Sachin Saxena Have a closer look into the if statement of your second loop . You are confusing i with j. And if you want ascending order you should only swap if a[j] > a[j + 1] if(a[j] > a[j+1]){ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } https://code.sololearn.com/cYj8Edaxrr0U/?ref=app
9th May 2021, 8:20 PM
Denise Roßberg
Denise Roßberg - avatar