public class Test{
public static void main(String[] args){
int[] a = {3, 2, 5, 21}; // created an array with 4 elements
int b,c;
for (b=0; b<=2; b++)//for loop that will have 3 iterations
{
if (a[b] < a[b+1])
{
c=a[b];//this
a[b] = a[b+1];//is
a[b+1] = c;//swapping
}
}
for(b=0; b<4; b++)
{
System.out.println(a[b]);
}
}
}
This outputs :
3
5
21
2
What I got when I was writing it down:
3
5
21
21
Could someone tell me how to approach it in my thoughts?