#Error in Selection Sort Algorithm

1 messages · Page 1 of 1 (latest)

tawny steppe
#

Hello. As the title states, in my personal project, I have designed a selection sort algorithm to organize times from earliest to latest. In order to store times, and name of a task, I made a multi-dimensional String ArrayList as follows:

private List<List<String>> tasks = new ArrayList<List<String>>();

After doing research, I ended up with my first attempt at a selection sort algorithm as follows:

private void sort() {
    int i = 0; int j = 0;
    for(List<String> task : tasks) {
      for(List<String> task2 : tasks) {
        if(Double.parseDouble(task2.get(1)) > Double.parseDouble(task.get(1))) {
          tasks.set(i, task);
          tasks.set(j, task2);
        }
        j++;
      }
      j = 0;
      i++;
    }
  }

With output:

Hello James. Here are your tasks for today -
Name of task: Dinner Time
Start and End times: 17.0-18.0
Name of task: Food Ordering
Start and End times: 8.0-10.0
Name of task: Boardroom Meeting
Start and End times: 11.0-15.0

I have fixed this issue using a different implementation, but I am still curious as to why this did not work with the for-each loop. Please let me know if you need more code for context. Thank you.

dreamy jungleBOT
#

<@&987246399047479336> please have a look, thanks.

tawny steppe
#

Yeah, debugging mode isn't making it clearer to me why this output occurs

tired epoch
#

Why are you storing times using strings

tawny steppe
#

Because I want to associate a name and a time. Due to my limited knowledge of data structures, I opted for an Array list using strings, since I can convert between string and doubles easy.

lilac remnant
#

Instead of using double try with HHMM format
eg 8.0 = 0800, 17.0 = 1700

#

You can use direct comparison

tawny steppe
#

but I am still curious as to why this version isn't working

rough spade
#

can you give sample data?

#

i.e. ```java
private List<List<String>> tasks = new ArrayList<List<String>>();

...
tasks.add(List.of(...));

#

then we can at least run your code

lilac remnant
#

the logic in your if condition is not doing anything

tawny steppe
#

okay, this is my sample input, which I used

#

"Dinner Time", 17, 18
"Food Ordering", 8, 10
"Boardroom Meeting", 11, 15

#

the goal of the "if" statement is to compare whether the task occurs sooner, compared to another

#

if it evaluates as true, then it will swap the ArrayList of Strings with each other

#

when debugging my program, it is showing that the if statement is evaluating as true

#

but even when 'task' and 'task2' meet the appropriate conditions for swap, it doesn't for some reason

tawny steppe
dreamy jungleBOT
tawny steppe
#

okay, I fixed it. It seems there was an issue with my use of 'i' and 'j'

#

I replaced the tasks.set(i, task); and the tasks.set(j, task2); with:

#
tasks.set(tasks.indexOf(task), task2);
tasks.set(tasks.indexOf(task2), task);
#

and the output is correct

#

still dunno how the iterations of i and j makes the program go hawire though

tired epoch
#

I would try to find a better way to store your 'tasks' better

rough spade
#
    private void sort() {
        int i = 0, j = 0;
        for(List<String> task : tasks) {
            for(List<String> task2 : tasks) {
                if(Double.parseDouble(task2.get(1)) > Double.parseDouble(task.get(1))) {
                    List<String> temp = tasks.get(i);
                    tasks.set(i, tasks.get(j));
                    tasks.set(j, temp);
                }
                j++;
            }
            j = 0;
            i++;
        }
    }
#

deleted my previous messages becuase I think it would just confuse you, so the TLDR is that your swap logic was wrong

#

i.e. ```java
List<String> temp = tasks.get(i);
tasks.set(i, tasks.get(j));
tasks.set(j, temp);

tawny steppe
#

mmm, so it's better not to use the for-each variable

#

or, better put, it's more clear not to

#

okay, thank you guys