0

So I am trying to make a mergesort program in java (specifically processing) that steps through the process frame by frame. When it sorted it in 1 frame by going through for loops it seemed to work fine (I didn't test it extensively as it wasn't my goal) but after making the changes to it necessary to have it function, it doesn't seem to want to work correctly.

I haven't tried a whole lot as I have absolutely no idea what could be causing this.

merge();

if (i1 < arr.length) {
  if (i2 < arr.length) {
    if (i3 <= i2 + i1) {
      i3++;
    } else {
      i2 += i1 * 2;
      l = i2 + 0;
      r = i2 + i1;
    }
  } else {
    i1 *= 2;
    i2 = 0;
    i3 = 0;
    l = 0;
    r = i1 + 0;

    arr = w.clone();
  }
} else {
  noLoop();
}

void merge() {
  int lMax = i2 + i1;
  int rMax = i2 + i1 * 2;

  if (r < arr.length - 1 && l < arr.length - 1) {
    if ((l <= lMax) && (r >= rMax || arr[l] <= arr[r])) {
      float[] t = arr.clone();
      w[i3] = t[l];
      arr = t.clone();
      l++;
    } else {
      float[] t = arr.clone();
      println(arr[r], t[r], w[i3]);
      w[i3] = t[r];
      println(arr[r], t[r], w[i3]);
      arr = t.clone();
      r++;
      println();
    }
  }
}

it should just sort the elements of arr but it seems to duplicate them whenever arr[r] < arr[l]

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • The best thing for you to do would be to step through this with a debugger. I'm sure you'll be able to see immediately what's going on. – Dawood ibn Kareem Apr 11 '19 at 19:24
  • @DawoodibnKareem how could I go about doing that? – lightningund Apr 11 '19 at 19:27
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner Apr 11 '19 at 19:29
  • Your IDE will have a debugger built in. If it's not obvious how to use it, there'll be instructions online somewhere. Google is your friend. – Dawood ibn Kareem Apr 11 '19 at 19:30
  • @DawoodibnKareem I stepped through it with the processing debugger but all I got was more confused – lightningund Apr 11 '19 at 19:47
  • Like others have said, you need to [debug your code](https://happycoding.io/tutorials/processing/debugging). Which line of code is behaving differently from what you expected? Can you narrow this down to a [mcve]? – Kevin Workman Apr 11 '19 at 19:52

1 Answers1

-1

I face this type of problem quite a lot in my work, and (if you're using Eclipse, at least, which is what I use) what I do is go into debug mode and put a breakpoint at every single line of code in the section in question and carefully watch the variables, changing the code accordingly. Hopefully that helps!