0
    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

nooonamee
  • 1
  • 1
  • 1
    You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Feb 22 '19 at 04:37
  • Debugger's the best idea or just a simple System.out.println of your counter and loop indexes as a quick look at what's going on. This is why homework is great. – strattonn Feb 22 '19 at 04:46

0 Answers0