2

I'm working on a sorting visualizer, I've been debugging this for hours. I'm trying to sort an array that is stored in the state.

I've setup a codesandbox here: https://codesandbox.io/s/pensive-villani-kdh17?file=/src/SortingVisualizer/SortingVisualizer.jsx

Can someone show me where I'm going wrong?

  mergeSort = async () => {
    const { array } = this.state;
    const arry = array.slice();
    const auxArray = arry.slice();
    await this.mergeSortHelper(arry, auxArray, 0, arry.length - 1);
  };

  mergeSortHelper = async (arry, auxArray, start, end) => {
    if (start === end) {
      return;
    }
    const middle = Math.floor((start + end) / 2);
    await this.mergeSortHelper(arry, auxArray, start, middle);
    await this.mergeSortHelper(arry, auxArray, middle + 1, end);
    await this.doMerge(arry, auxArray, start, middle, end);
  };

  doMerge = async (arry, auxArray, start, middle, end) => {
    let a = start; //arry start
    let b = start; //auxArray start
    let c = middle + 1; //mid start

    while (b <= middle && c <= end) {
      if (arry[b].height <= arry[c].height) {
        arry[a] = auxArray[b];
        this.setState({ array: arry });
        await sleep(ANIMATION_SPEED_MS);
        a++;
        b++;
      } else {
        arry[a] = auxArray[c];
        this.setState({ array: arry });
        await sleep(ANIMATION_SPEED_MS);
        a++;
        c++;
      }
    }

    while (b <= middle) {
      arry[a] = auxArray[b];
      this.setState({ array: arry });
      await sleep(ANIMATION_SPEED_MS);
      a++;
      b++;
    }

    while (c <= end) {
      arry[a] = auxArray[c];
      this.setState({ array: arry });
      await sleep(ANIMATION_SPEED_MS);
      a++;
      c++;
    }
    auxArray = arry.slice();
  };
3
  • I may be wrong, but I asked a similar question yesterday: stackoverflow.com/questions/65689646/… Commented Jan 13, 2021 at 9:13
  • I haven't seen that question but I'll look through it now Commented Jan 13, 2021 at 9:14
  • No, I'm having no problem re-rendering my array with new data Commented Jan 13, 2021 at 9:18

2 Answers 2

1

I think you don't need all those complecated functions, just add these to function to your code and use them:


  increasingSort = (inputArray) => inputArray.sort((a, b) => a.height - b.height)

  decreasingSort = (inputArray) => inputArray.sort((a, b) => b.height - a.height);

check this: https://codesandbox.io/s/pedantic-architecture-vc4ox

Sign up to request clarification or add additional context in comments.

6 Comments

I'm trying to make a visualizer for the actual sorting
yes, how would I show the actual sorting steps? did you look at my demo?
what do you mean by actual sorting steps? yes I've just added the two functions above and showed you in the link above that how you could use it. did you see the demo that it works and data is sorted?
a SORTING VISUALIZER, it would show the steps of the merge sort on a random array to a sorted array. I'm aware I can just use library functions automatically get a sorted array.
@Sujio I'm not getting your point. have a good day
|
0

The bug was that the auxArray and arry was different after changing it and recursive calling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.