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: Fill & sort an array

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Fill & sort an array

    Hello everyone. Thank you in advance for your time. I want this program to generate user defined amount of random number between 0-999 and then sort them in ascending order. It works properly without the bubble sort, but when i add the bubble sort it gives me a ridiculous output. like below

    Output

    Please Enter the amount of random numbers you want to generate : 2

    229
    Sorted array
    229
    891
    Sorted array
    891

    ///////////////////


    Output I need

    Please Enter the amount of random numbers you want to generate : 3

    229
    45
    251

    Sorted array

    45
    229
    251




    My code so far
    package random;
     
    import java.util.Random;
     
    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
     
            Scanner in = new Scanner(System.in);
            System.out.print("Please Enter the amount of random numbers you want to generate :");
     
            int input = in.nextInt();
     
            Random dice = new Random();
            int number;
     
            for (int counter = 1; counter <= input; counter++) {
                number = 1 + dice.nextInt(999);// Randomly generated values between 0-999
     
     
                {
                    int nums[] = {number}; // Passing randomly generated values to array
     
     
     
     
     
                    int size = nums.length;
     
                    for (int i = 0; i < size; i++) {
                        System.out.println(nums[i]);// Display original array
                    }
     
     
     
    // Sorting 
     
                    for (int i = 0; i < size; i++) {
     
                        for (int j = i + 1; j < size; j++) {
     
                            if (nums[i] < nums[j]) {
     
                                int temp = nums[i];
                                nums[i] = nums[j];
                                nums[j] = temp;
                            }
                        }
                    }
     
                    //Display sorted array
     
                    System.out.println("Sorted array");
                    for (int i = 0; i < size; i++) {
                        System.out.println("" + nums[i]);
                    }
                    System.out.println();
     
     
                }
            }
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Fill & sort an array

    Is anybody there to help me??

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Fill & sort an array

    You need to look at how you first generate random numbers and fill your array.

    You simply generate one number, create a new array with the number, sort the array, then do the same thing again.

    You should change your algorithm,

    get number of values
    while i is less than no:
          add new random to array
          i + 1
    end while
     
    sort array

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 22nd, 2011, 08:53 AM
  2. can't figure out how to sort an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 02:07 PM
  3. Fill array concurrently
    By mamba in forum Collections and Generics
    Replies: 2
    Last Post: October 15th, 2009, 07:08 PM
  4. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM