1
import java.util.Random;

public class Tester {
    public static void main(String[] args) {
        selectionSort(args);
    }

    private static void printArray(int[] anArray) {
        for (int i = 0; i < anArray.length; i++) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(anArray[i]);
        }
    }


    public static void selectionSort(String[] args) {

        int i, x = 0;

        int l = 10;
        Random r = new Random(271);
        int array[] = new int[l];
        for (i = 0; i < l; i++) {
            array[i] = r.nextInt();
        }

        while (i < l) {

            for (int j = 1; j <= i; j++) {
                if (array[j] < array[x])
                    x = j;
            }

            i++;
        }
        printArray(array);
    }

}

Everything is fine I just cannot print it correctly. When I print I get "-1061221096, -349834974, -1279215928, 1452441141, -367008517, 638966200, -464597014, 1551135748, -446923224, 542496703" which is not correct. Each number should be under 271 I believe.

4
  • What is your input? and why are you passing args to selectionSort method? Commented Feb 3, 2016 at 23:48
  • There is no input, I should only be getting output. I'm suppose to be passing (int[] arr) to the selection sort, but with that I am getting errors. Commented Feb 3, 2016 at 23:51
  • Random has an internal seed and algorithm which requires you to use a single instance for a group of values, using multiple instances can generate duplicate values or even values in the same order Commented Feb 3, 2016 at 23:55
  • related: stackoverflow.com/questions/5827023/… Commented Feb 13, 2016 at 8:56

3 Answers 3

2

Try to replace this:

 Random r = new Random(271);

With:

 Random r = new Random();

And replace this:

 array[i] = r.nextInt();

With:

 array[i] = r.nextInt(271);

You can have output something like this under 271:

  18, 94, 189, 105, 32, 153, 68, 159, 178, 34
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Please rate this up if you can, I am under 15 karma.
1

You have the line

Random r = new Random(271);

Now, every time I've used Random, I've always done it with

Random r = new Random();
int rand = r.nextInt(271);

That should work as expected.

And just from a code cleanup perspective, you do not need to pass args[] to selectionSort(). You can simply make the method have no parameters and pass nothing from the main method.

1 Comment

Thank you, please rate this guy up since I am under 15 karma.
0
while (i < l){
    for(int j=1; j<=i; j++){
        if(array[j] < array[x])
            x = j;
    }

    i++;}

this bit of code won't get called because 'i' is equal to 'l' because of the previous loop. the 271 is called the seed and it won't give you arandom numbers less than 271, in order to generate numbers less than 271 you should use r.next(271) see the API Documentation Random API JSE7

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.