2D Array Bubblet Sorting

Hello. For my school project, I need to create 10x15 2D Array (Row x Column) and fill it with random odd numbers between -51 and 99.

Then I need to sort the list using the Bubble sort.

So far, I've gotten the basic layout done but I'm having trouble with Bubble sorting and randomize equation.

This is what I have for randomizing odd numbers between -51 and 99 but it generates even numbers as well for some reason.

for (int k = 0; k < 10; k ++) {

for (int j = 0; j < 15; j ++) {

twoD[k][j] = (int) (((Math.random () * 76)-25) *2)-1;

}

}

This is what I have for Bubble sorting a 2D 10 x 15 array but it gives me array out of bound error and exec error.

public void modBubbleSorting (){

int temp;

int temp1;

for (int count = 0; count < 150; count++) {

for (int c = 0; c < 10; c++) {

for (int d = 0; d < 15; d++) {

if (d==14) {

if (twoD[c][d] > twoD[c+1][0]) {

temp = twoD[c][d];

twoD[c][d] = twoD[c+1][0];

twoD[c+1][0] = temp;

}

}

else if (d!=14) {

if (twoD[c][d] > twoD[c][d+1]) {

temp1 = twoD[c][d];

twoD[c][d] = twoD[c][d+1];

twoD[c][d+1] = temp1;

}

}

}

}

}

}

Could somebody tell me which part of these codes are wrong?

Thanks alot!

[1377 byte] By [heaton17a] at [2007-10-2 9:50:26]
# 1

> for (int k = 0; k < 10; k ++) {

>for (int j = 0; j < 15; j ++) {

> twoD[k][j] = (int) (((Math.random () * 76)-25)

> 5) *2)-1;

>}

> }

>

>

> This is what I have for Bubble sorting a 2D 10 x 15

> array but it gives me array out of bound error and

> exec error.

How did you initialise your twoD array?

fverhoefa at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...
# 2
This is how I initialized itint twoD[][] = new int [10][15];
heaton17a at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...
# 3

As for your random number generation, it is not strange that you get even numbers.

> for (int k = 0; k < 10; k ++) {

>for (int j = 0; j < 15; j ++) {

> twoD[k][j] = (int) (((Math.random () * 76)-25)

> 5) *2)-1;

>}

> }

Math.random() gives a double between 0.0 and 1.0 (excluding 1 itself). Then this double you multiply by 76, getting a double between 0.0 and 76.0. This your subtract by 25, getting a double btween -25.0 and 51.0. This you multiply by 2, getting a double between -50.0 and 102.0. This you cast to an int after which you subtract it by one. As you do the cast to the int so late, the rounding will cause that some even numbers will slip through.

Maybe you should take a look at Random.nextInt(int), which would make your life easier.

Frans

fverhoefa at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks, could you possibly tell me how to work Random.nextInt(int) ? And do you have any idea what is wrong with my Bubble Sort?Thanks
heaton17a at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...
# 5
to get a integer in the range 0-75, you just need to do:Random rand = new Randomint randomNumber = rand.nextInt(76);
fverhoefa at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...
# 6

If you have to sort a multi dimensional array as a single data set, pour them into a one

dimensional array and sort it before backing the sorted data onto the original multi

dimensional array. That is:

Sort a simple one dimensional array -> Distribute the result onto the multi dimensional arry

import java.util.*;

import java.text.*;

public class Heaton17{

public static void main(String[] args){

DecimalFormat df = new DecimalFormat("##");

int base = -52;

int range = 99 - base;

Random rand = new Random();

int[] oned = new int[10 * 15];

// odd numbers between -51 and 99

for (int i = 0; i < oned.length; ++i){

int r = rand.nextInt(range + 1); // generate 1 to 151

r += (r % 2 == 1)? 0 : 1; // 2 -> 3, 4 -> 5, etc.

oned[i] = base + r;

}

// bubble sort

for (int i = 0; i < oned.length - 1; ++i){

for (int j = i + 1; j < oned.length; ++j){

if (oned[i] > oned[j]){

int temp = oned[i];

oned[i] = oned[j];

oned[j] = temp;

}

}

}

int v;

String sign, num;

int k = 0;

int[][] twod = new int[10][15];

// make a two dimensional array

for (int i = 0; i < twod.length; ++i){

for (int j = 0; j < twod[i].length; ++j){

v = twod[i][j] = oned[k++];

sign = (v < 0)? "" : (v == 0)? " " : "+";

num = df.format(v);

if (num.length() == 1){

num = " " + num;

}

else if (v < 0 && num.length() == 2){

num = num.charAt(0) + " " + num.charAt(1);

}

System.out.print(sign + num + " ");

}

System.out.println();

}

}

}

hiwaa at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...
# 7
...Wel..., are they implicitly talking about merge sort?
hiwaa at 2007-7-16 23:55:36 > top of Java-index,Java Essentials,Java Programming...