I have the following algorithm to order an .txt file with 10 numbers
for (int i=0;i<array.length;i++)
{
for(int j=i;j<array.length;j++)
{
if (array[i]<array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
And it writes a new .txt file with all numbers in order. But with pen an paper it says it shouldn't work. It's the following:
7 10 4 3 5 8 1 3
The algorithm should do this:
10 7 4 3 5 8 1 3
10 8 4 3 5 7 1 3
10 8 5 3 4 7 1 3
10 8 5 4 3 7 1 3
10 8 5 4 7 3 1 3
10 8 5 4 7 3 3 1
Clearly, last line it's not in order, so why is the code doing it right? Or... where am I wrong when I doing it with pen and paper?
i = 0till end. Pretty basic sorting algorithm with complexity of O(n2)