public int[] sortedSquares(int[] nums) {
int size = nums.length;
int result[] = new int[size];
for(int i=0; i<size-1; i++)
{
int min =i;
for(int j=i+1; j<size; j++)
if(nums[j]<nums[min])
min = j;
int temp = nums[min];
nums[min] = nums[i];
nums[i]=temp;
}
for(int i=0; i<size; i++)
{
result[i] = nums[i]*nums[i];
}
return result;
}
this is from leetcode question "Squares of a Sorted Array" I sorted it and the result come out not sort. i dont know what happen? if i use Arrays.sort(result) it will work, but why this code not working?


min = jmust be insideifandfor, swaping should be done after the loop - the code with black background is correctly indented) ¹ you can useabsbefore sorting, but that will make code even harder to understand, and no need, just sort after squaring (squares should sorted, so sort the squares {if input should be squared then sort the input}){}forif,else,for,while, ... and let the IDE indent your code (if you don't want to do it)