Random number generator using bubble sort

Generate 50 Numbers between 1 and 100 and sort them using bubble sort

Page 1 of 1

3 Replies - 9742 Views - Last Post: 06 May 2008 - 03:55 PM Rate Topic: -----

#1 madkidz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • 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) {)

:)
Was This Post Helpful? 0
  • +
  • -

#3 madkidz  Icon User is offline

  • New D.I.C Head

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

Re: Random number generator using bubble sort

Posted 06 May 2008 - 03:52 PM

View PostMartyr2, 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?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • 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? ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1