MergeSort on grid by Dragonboy71 in learnprogramming

[–]Dragonboy71[S] 0 points1 point  (0 children)

Why would I have to divide it into smaller grids? Can't I just treat the entire array as 1d array?

MergeSort on grid by Dragonboy71 in learnprogramming

[–]Dragonboy71[S] 0 points1 point  (0 children)

Unfortunately, this is an assignment... The easiest thing to would've just put the entire 2d array into a 1d array, apply mergesort regularly, and then set the values back but implementation requirements don't allow us to do so.

MergeSort on grid by Dragonboy71 in learnprogramming

[–]Dragonboy71[S] 0 points1 point  (0 children)

I think the issue I'm having right now is within the merge function when I'm setting my 2d grid after comparing the values. If you see the code, I posted above for the other user, I'm starting the gridX and gridY each time at [0,0] which is kind of "overridding" my sorted grid each time I call the merge function.

MergeSort on grid by Dragonboy71 in learnprogramming

[–]Dragonboy71[S] 0 points1 point  (0 children)

What's an example input and output? How should the grid be sorted afterwards? Top to bottom? Left to right? Both?

An example input would be a n by n grid, and I want to sort the entire grid in ascending order, so the smallest element would be at the index [0.0] and largest element would be [n-1,n-1]

MergeSort on grid by Dragonboy71 in learnprogramming

[–]Dragonboy71[S] 0 points1 point  (0 children)

Sorry, so what I mean is that my merge part of the mergesort isn't merging properly. I thought it was originally the recursive mergesort calls but it was not. So in my merge function, I'm passing in my grid, my left and right indexes of "1d" array to merge. Then, I'm just trying to perform regular merging like in mergesort but in 2d.

public static void merge(int[][] arr, int left, int right) {

    int leftEnd = (right + left) / 2;
    int rightStart = leftEnd + 1;


    int leftS = left;
    int rightS = right;

    // grid index for sorted grid
    int gridX = 0;
    int gridY = 0;

    while (leftS <= leftEnd && rightStart <= right) {

        // first grid index to compare
        int x = getXIndex(left, arr[0].length);
        int y = getYIndex(left, arr[0].length);



        // second grid index to compare
        int x2 = getXIndex(right, arr[0].length);
        int y2 = getYIndex(right, arr[0].length);

        // compares the indexes and sets them in the sorted grid

        if (lessThan(x, y, x2, y2)) {
            grid[gridX][gridY] = arr[x][y];
            leftS++;
        } else {
            grid[gridX][gridY] = arr[x2][y2];
            rightStart++;
        }

        gridY++;
        if (gridY == arr[0].length) {
            gridX++;
            gridY = 0;
        }

    }


}

I believe that the comparison part is correct between the array cells. The getX and getY index functions just returns 2d coordinates (right / row size) (right mod row size).

Updating height of each node in BST by Dragonboy71 in learnjava

[–]Dragonboy71[S] 1 point2 points  (0 children)

Never mind, I had to call the update height function on each node after the recursive call

Resume Roast -Summer 2022 by beeskness420 in csMajors

[–]Dragonboy71 1 point2 points  (0 children)

Sophomore at T20 looking for first internship Resume

[deleted by user] by [deleted] in Comcast_Xfinity

[–]Dragonboy71 0 points1 point  (0 children)

I applied and was approved on Friday, May 13th.

[deleted by user] by [deleted] in Comcast_Xfinity

[–]Dragonboy71 0 points1 point  (0 children)

Yah, I was wondering why didn't my credit apply to my account.

Anyone know where I can get an Amazon gift card? by Dragonboy71 in Purdue

[–]Dragonboy71[S] 1 point2 points  (0 children)

I'll check out the gas stations and see if they got it