I suspect an error in your code, it should not compile at all.
int ar[] = 7, 5, 2, 8, 4, 9, 6 };
and there is another error in
int sorted[] = new int[0];
you need to give the size of
sorted
otherwise, you should try to replace
smallest = Integer.MAX_VALUE;
with
ar[index] = Integer.MAX_VALUE;