0

I need help debugging my code. It keeps on making a mistake and I can't find out where. Here is my selection sort method:

private void selectionSort() {
        int best = 0;
        int j = 0;
        SortableRobot bestSoFar = botList.get(0);
        for(int i = 0;i<botList.size();i++) {
            int[] temp = botList.get(j).getLocation();
            for(int x = j;x<botList.size();x++) {
                if(botList.get(j).compareTo(botList.get(x)) < 0) {
                    // botList.get(j).moveToLocation(botList.get(x).getLocation());
                    // botList.get(x).moveToLocation(temp);
                    bestSoFar = botList.get(x);
                    best = x;
                }
            }
            SortableRobot tempbot = botList.get(j);
            botList.set(best,tempbot);
            botList.set(j, bestSoFar);
            j++;
        }
    }

2 Answers 2

1

The problem is that the variable bestSoFar has to be set in the start of every iteration.

This edit made it working in my tests:

import java.util.ArrayList;
import java.util.List;

public class Test {

    private List<SortableRobot> botList;

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        botList = new ArrayList<SortableRobot>();
        botList.add(new SortableRobot(5));
        botList.add(new SortableRobot(3));
        botList.add(new SortableRobot(4));
        botList.add(new SortableRobot(1));
        botList.add(new SortableRobot(2));

        System.out.println("before sort: " + botList);
        selectionSort();
        System.out.println("after sort:  " + botList);
    }

    private void selectionSort() {
        int best = 0;
        int j = 0;
        SortableRobot bestSoFar = botList.get(0);
        for (int i = 0; i < botList.size(); i++) {
            bestSoFar = botList.get(j);// EDITED HERE the best bot so far has to be set to the current bot in the beginning of every iteration
            // int[] temp = botList.get(j).getLocation();
            for (int x = j; x < botList.size(); x++) {
                if (botList.get(j).compareTo(botList.get(x)) < 0) {
                    // botList.get(j).moveToLocation(botList.get(x).getLocation());
                    // botList.get(x).moveToLocation(temp);
                    bestSoFar = botList.get(x);
                    best = x;
                }
            }
            SortableRobot tempbot = botList.get(j);
            botList.set(best, tempbot);
            botList.set(j, bestSoFar);
            j++;
        }
    }

    private class SortableRobot implements Comparable<SortableRobot> {

        private int sortIndex;

        public SortableRobot(int sortIndex) {
            this.sortIndex = sortIndex;
        }

        public String toString() {
            return "SortableRobot[" + sortIndex + "]";
        }

        public int compareTo(SortableRobot o) {
            return Integer.compare(sortIndex, o.sortIndex);
        }
    }
}

The output is:

before sort: [SortableRobot[5], SortableRobot[3], SortableRobot[4], SortableRobot[1], SortableRobot[2]]
after sort:  [SortableRobot[5], SortableRobot[4], SortableRobot[3], SortableRobot[2], SortableRobot[1]]
0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* * Select 1 element index-wise and compare with all element up to last index. if next value is greater than compare value then swap these 2 value * and do same until we get last index. */

public class SortingSelection {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(Arrays.asList(40, 10, -30, 45, 39, 32));

        for (int i = 0; i < list.size(); i++) {
            int selectionIndex = i;

            for (int j = selectionIndex + 1; j < list.size(); j++) {
                if (list.get(selectionIndex) > list.get(j)) {
                    int temp = list.get(selectionIndex);
                    list.set(selectionIndex, list.get(j));
                    list.set(j, temp);

                }
            }
            System.out.println(list);
        }
    }

}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.