I have a homework to sort an array in ascending order. Obviously, this must be done manually without using any function sort().
I decided to do this, I will need two cycles for: the first will go through the existing array and create a temporary value with the value and index of the array. The second cycle will compare time values with existing values and sort them. I try to write code all the time, but I just can't figure out how to do it right. Here is the last method I came up with:
public int[] sortArray (int[] inArray)
{
int[] newArray = inArray;
for(int x = 0; x < a.length; x++)
{
int tempValue = a[x];
int tempIndex = x;
for(int y = 0; y < a.length; y++)
{
if(tempValue < a[y])
{
newArray[x] = tempValue;
}
}
}
return newArray;
}
I am sure that this is wrong, but if someone can push me in the right direction, it will be very grateful!
source