0

I am trying to sort an array of random numbers without using the arrays.sort. I have the code, but it doesn't work. not sure where is the error. Any kind of help is appreciated.

import java.util.*;
public class Sort
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.print("How many numbers do you want? ");
        int howMany = in.nextInt();
        int [] myArray =  getRandomArray(howMany);

    }

    /* public static int bsearch(int[] arr, int key)
    {

    }*/

    public static int[] getRandomArray(int howMany) {
        int[] returnMe = new int[howMany]; // Assume size >= 0
        Random rand = new Random();
        for (int i = 0; i < howMany ; i++) 
        returnMe[i] = rand.nextInt(Integer.MAX_VALUE) + 1;
        //System.out.print(returnMe[i] + " ");
        for (int i = 1; i <= (howMany - 1); i++)
        {
            for (int j = 0; j < howMany - i -1; j++) 
            {
                int tmp = 0;
                if (returnMe[j] > returnMe[j+1])
                {
                    tmp = returnMe[j];
                    returnMe[j] = returnMe[j + 1];
                    returnMe[j + 1] = tmp; 
                }   
            }  
        }
        for ( int i = 0; i < howMany; i++)
            System.out.println(returnMe[i] + " "); 
        return returnMe;
    }
}
7
  • "Does not work" means what, exactly? Does not compile? Compiles, but does not run? Compiles and runs be emits an error (which?) or other incorrect output? Commented Apr 18, 2017 at 20:45
  • It compiles, run and gives output, but not in an ascending order(just random order) @JohnBollinger Commented Apr 18, 2017 at 20:47
  • You do realize, do you not, that you need to do more than name your class "Sort" to get it to perform actual sorting? Because I don't see anything whatever in it that would do any sorting. Commented Apr 18, 2017 at 20:47
  • @DiabolicWords I am using BlueJ and it is not sorting it in ascending order. Commented Apr 18, 2017 at 20:48
  • @JohnBollinger I have the coding in the getRandomArray method, it is the bubble sort method. You say , that it is wrong? Commented Apr 18, 2017 at 20:50

1 Answer 1

1

Your line

        for (int j = 0; j < howMany - i -1; j++) 

should be

        for (int j = 0; j <= howMany - i -1; j++) 

or alternatively, remove the "-1" and keep "<". Otherwise, you will ignore the last number in the array. Everything else looks fine to me.

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help :). Also, you could consider renaming the method to something like createRandomSortedArray, since createRandomArray implies its a yet-to-be sorted array. However, thats just to make the code easier to read&understand for others (which can actually be quite important if you don't just code for yourself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.