Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Bubble Sort with random numbers

1. ## 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:
```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!

2. ## Re: Bubble Sort with random numbers

You should be held back a year in school there...

But so that does not happen....

```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.

3. ## Re: Bubble Sort with random numbers

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:
The Problem with Spoon-feeding