Sajad
Sajad

Reputation: 2363

Swap two- dimensional array elements

This is my FCFS algorithm.

I have a two dimensional array called atst means arrival time, service time.

Array size is 5*5.

The logical picture of my array is this: enter image description here

First column : process name

Second column : arrival time

Third column : service time (burst time)

And the Fourth and Fifth columns will be initialize for waiting time and total time after sort array by arrival time (second column).

My problem is in sorting array by second column (arrival times) by ascending order.

My code:

    // Sort array by arrival times
    int[] temp = new int[3];
    for (int i = 0; i < atst.length; i++) {
        if (atst[i][1] > atst[i + 1][1]) {     // Then swap!
            temp[i] = atst[i][1];
            atst[i][1] = atst[i + 1][1];
            atst[i + 1][1] = temp[i];
        }
    }

I use temp[3] because before of sorting other columns (fourth and fifth) are empty(zero).

But this code make arrayOutOfBoundException in this line: temp[i] = atst[i][1]; .

How fix this problem?

Upvotes: 0

Views: 4078

Answers (2)

tod
tod

Reputation: 1575

Here you go:

 // Sort array by arrival times
int[] temp = new int[atst.length]; //to avoid any out of bound.
for (int i = 0; i < atst.length-1; i++) { //-1 will solve
    if (atst[i][1] > atst[i + 1][1]) {     // Then swap!
        temp[i] = atst[i][1];
        atst[i][1] = atst[i + 1][1];
        atst[i + 1][1] = temp[i];
    }
}

Hope this will work. you have been exceeding the array bound in atst[i+1] for the last value of i. This -1 will solve your problem of arrayOutOfBoundException.

EDIT: Try this:

 // Sort array by arrival times
int[] temp = new int[atst.length]; //to avoid any out of bound.
for (int i = 0; i < atst.length-1; i++) { //-1 will solve
    for (int j = 0; i < 2; j++) {
        if (atst[i][j] > atst[i + 1][j]) {     // Then swap!
           temp[i] = atst[i][j];
           atst[i][j] = atst[i + 1][j];
           atst[i + 1][j] = temp[i];
        }
    }
}

Upvotes: 1

Gene
Gene

Reputation: 47020

Your design is broken in several ways.

First, you ought not to be using a 2d array at all. Since the columns have different meanings, an array of objects containing named fields will save you much work in the long run.

Second, what you're showing isn't close to a sort algorithm. Even after fixing the coding problems, a single pass over an array is not sufficient to sort it. Look at Arrays.sort().

Third, your code must be broken by even cursory inspection. The variable i iterates from 0 to atst.length - 1. Yet you've allocated temp to have only 3 positions. Unless atst.length is certain to be no more than 3, you'll run off the end of temp with a range error.

Fourth, you want to sort rows. The code it looks like you're trying to write will swap only the second column of each row. The rest of the array will remain as-is. I don't think that's what you want.

Take a step back. Think. Throw this code away. Try again.

Upvotes: 1

Related Questions