Question: 1 import java.util.*; 2 3 public class Test7 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8

1 import java.util.*; 2 3 public class Test7 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 int[] array = new int[6]; 10 11 for(int i=0; i<6; i++) 12 { 13 array[i] = input.nextInt(); 14 } 15 16 int c = 0; 17 18 for(int i=0; i<6; i++) 19 { 20 for(int j=i+1; i<6; i++) 21 { 22 if(array[i]>array[j]) 23 { 24 c = array[i]; 25 array[i] = array[j]; 26 array[j] = c; 27 } 28 } 29 } 30 31 for(int i=0; i<6; i++) 32 { 33 System.out.println(array[i]); 34 } 35 } 36 37 38 }

I'm trying to sort an array in aescending order. If i input something like 5, 3, 6, 1, 2 it only swaps the first two (output: 3, 5, 6, 1, 2).

I can't figure out what the problem is.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!