# Bubble Sort with random numbers

• August 19th, 2011, 10:36 PM
jake6047
Bubble Sort with random numbers
Hi I am pretty new to java and one of my assignments in university is causing me a problem and I just can't seem to figure out where i'm going wrong.

My specification is:

1] Creating a real random number between "0" and "20" using the "Random" constructor with a seed of 123456
then
2] Creating an array of 30000 different random numbers using this seed
then
3] Using a bubble sort (without using the "java.util.Arrays.sort" or "java.util.Collections.sort" methods) to identify the highest 10 elements of that array.
then
4] Printing out those 10 figures to screen.

The code I have made so far is:
Code :

```import java.util.Random;     public class BubbleSort{ public static void main(String a[]){ Random rndNumbers = new Random(); int rndNumber = 0;   for (int nbr = 0; nbr < 10; nbr++) { rndNumber = rndNumbers.nextInt(3000); System.out.println("Number: " + rndNumber); int[] x = new int[6]; for (int i=0; i<x.length; i++); int i; int array[] = {rndNumber}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("The bubble sort algorithm has put the random integers into order!"); } }   public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; } } } } }```

For some reason it is only generating one random number at a time. Can anybody help me please?:-?
Any help would be great, thanks!
• August 19th, 2011, 11:39 PM
Spidey1980
Re: Bubble Sort with random numbers
You should be held back a year in school there...

But so that does not happen....

Code java:

```import java.util.Random; ...edited by moderator```

Notice how I used just one array. Also you were seeding 3000 and creating random numbers from 1 to 123456. this has all been fixed.

Now I'm gonna get yelled at for spoonfeeding you. I don't care. I was bored.

Note: I've been doing java for only 14 days... I started programming in basic in 1986.
• August 20th, 2011, 10:39 AM
copeg
Re: Bubble Sort with random numbers
Quote:

Originally Posted by Spidey1980
Now I'm gonna get yelled at for spoonfeeding you. I don't care. I was bored.

Yes you are. And your post has been edited
I recommend you read the forum rules, and the following:
http://www.javaprogrammingforums.com...n-feeding.html