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!