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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Bubble Sort with random numbers

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default 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.
    Last edited by copeg; August 20th, 2011 at 09:39 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Bubble Sort with random numbers

    Quote Originally Posted by Spidey1980 View Post
    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

Similar Threads

  1. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 08:47 PM
  2. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  3. Javascript Bubble Sort
    By Fidelacchius in forum What's Wrong With My Code?
    Replies: 17
    Last Post: May 24th, 2010, 04:43 PM
  4. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 08:22 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM

Tags for this Thread