int counter = 0;
int min;
int temp2;
System.out.println("\n\n2. Selection Sort"); // selection
double startTime1 = System.nanoTime();
for (int i = 0; i < list2.size(); i++)
{
min =i;
for (int k = i+1; k < list2.size(); k++)
{
counter++;
if ( list2.get(min) > list2.get(k))
{
min = k;
}
}
temp2 = list2.get(min);
list2.set(min,list2.get(i));
list2.set(i,temp2);
}
double endTime1 = System.nanoTime();
double duration1 = endTime1 - startTime1;
System.out.println("Seconds to sort = " + duration1/1000000000 + " seconds");
System.out.println("Number of iterations = " + counter);
I'm working on several loops for my homework, I don't know why my counter is negative in this case. With bubble sort, the counter is put in the same place like that but it returns a positive result. Can somebody enlighten me? Thank you in advance