I've been working on a method that sorts an array from lowest to highest number. I came up with the code below but, as you probably guessed, it doesn't do what I expected.
The logic I wished to apply goes as follows:
Given I have an array, for example array {4,3,1,2,5} The code would compare, for example, array[0] (which would be four in this case) with each element in the array,
array[0]>array[0] (4>4=false),
array[0]>array[1] (4>3)= +1counter,
array[0]>array[2] (4>1)= +1counter,
array[0]>array[3] (4>2)= +1counter,
array[0]>array[4] (4>5=false)
counter = 3
So since the counter value its now 3, in the new array (array2, or arrayOrdered) number 4 would be in the third index.
How should I fix it? Any help its more than appreciated!
public static int[] OrderArray(int[] array)
{
int[] array2=new int[array.length];
for (int i=0; i<array.length; i++)
{
int place=0;
for (int j=0; j<array.length;j++)
{
if (array[i]> array[j])
{
place = place+1;
}
array2[place] = array[i];
}
}
return array2;
}