I am not able to understand the internal working that is happening behind the screen but I am confused because it is printing different result in console than what it is expected to. Could anybody resolve my problem with good explanation?
public class Demo {
public int [] sort(int h[])
{
int temp;
for(int i=0;i<h.length;i++)
{
for (int j=i;j<h.length;j++)
{
if(h[i]>h[j])
{
temp=h[i];
h[i]=h[j];
h[j]=temp;
}
}
}
return h;
}
public static void main(String args[])
{
Demo obj =new Demo();
int a[]={9,2,3,5,32,1,5,3,7};
int[] sorted=obj.sort(a);
/*Code to Display Array a*/
for(int s :a)
{
System.out.print(s+ " ");
}
System.out.println("");
/*Code to Display Array sorted*/
for(int k:sorted)
{
System.out.print(k+" ");
}
}
/*
Expected output
9 2 3 5 32 1 5 3 7
1 2 3 3 5 5 7 9 32
Actual output
1 2 3 3 5 5 7 9 32
1 2 3 3 5 5 7 9 32
*/
}