# Random number generator using bubble sort

Page 1 of 1

## 3 Replies - 10598 Views - Last Post: 06 May 2008 - 03:55 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=51248&amp;s=280bf87ef54ab10aa86cca44fbe353bd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 2
• Joined: 06-May 08

# Random number generator using bubble sort

Posted 06 May 2008 - 03:24 PM

I cant figure out why I am getting a compilation era. I am suppose to randomly generate 50 numbers between 1 and 100 using bubble sort. When I run it I get a syntax era. I am very new to java programming and I have been teaching myself most of the coding. My professor is of little to know help with a project I am trying to do.
I need to know where I am going wrong with this.
I get it to run but I can't get it to sort.

package CSCI;

public class RandomArray {

/** The method for sorting the numbers */
public static void bubbleSort(int[] randomNums) {
boolean needNextPass = true;

for (int k = 1; k < randomNums.length; k++) {
// Array may be sorted and next pass not needed
needNextPass = false;
for (int i = 0; i < randomNums.length - k; i++) {
if (randomNums[i] > randomNums[i + 1]) {
// swap list[i] with list[i + 1]
int temp = randomNums[i];
randomNums[i] = randomNums[i + 1];
randomNums[i + 1] = temp;

needNextPass = true; // Next pass still needed
}
}
}
}

public static void main(String[] args) {
int [] randomNums = new int [20];

for (int i = 0; i < randomNums.length; i++){
randomNums[i] = (int) (100 * Math.random());
}

for (int i = 0; i < randomNums.length; i++) {
System.out.print(randomNums[i]+ ",");
}
}
}

This post has been edited by madkidz: 06 May 2008 - 03:40 PM

Is This A Good Question/Topic? 0

## Replies To: Random number generator using bubble sort

### #2 Martyr2

• Programming Theoretician

Reputation: 4664
• Posts: 12,755
• Joined: 18-April 07

## Re: Random number generator using bubble sort

Posted 06 May 2008 - 03:47 PM

You are just forgetting a closing curley brace for the entire program. Then secondly, you need to call your bubbleSort function and give it your randomNums array.

Once you make those two changes, you will have a fully functioning program. Oh and one last note, you set your boolean for making passes in the bubblesort, but you never actually use it. If you have to use it, then your bubbleSort will need to test if another pass needs to be done. (aka while (needNextPass) {)

Reputation: 0
• Posts: 2
• Joined: 06-May 08

## Re: Random number generator using bubble sort

Posted 06 May 2008 - 03:52 PM

Martyr2, on 6 May, 2008 - 03:47 PM, said:

You are just forgetting a closing curley brace for the entire program. Then secondly, you need to call your bubbleSort function and give it your randomNums array.

Once you make those two changes, you will have a fully functioning program. Oh and one last note, you set your boolean for making passes in the bubblesort, but you never actually use it. If you have to use it, then your bubbleSort will need to test if another pass needs to be done. (aka while (needNextPass) {)

Sorry for my ignorance, but how do you call bubbleSort?

### #4 Martyr2

• Programming Theoretician

Reputation: 4664
• Posts: 12,755
• Joined: 18-April 07

## Re: Random number generator using bubble sort

Posted 06 May 2008 - 03:55 PM

After your first for loop in the main function write...

bubbleSort(randomNums);

Not too hard now is it?