forum

Home / DeveloperSection / Forums / sorting arrays in java

sorting arrays in java

Barbara Jones 2036 13-Nov-2014

I`ve been trying to bubble sort an array in java for some time now, but every time it just does a few and then it follows the array list.

public void mysort() {
    Random randomNumbers = new Random();
    int randomArray[] = new int[20];
    for (int j = 0; j <= 19; j++) {
        randomArray[j] = randomNumbers.nextInt(200 + 1);
        for (int k = 1; k < 20; k++) {
            if (randomArray[k - 1] < randomArray[k]) {
                int hjelp = randomArray[k - 1];
                randomArray[k - 1] = randomArray[k];
                randomArray[k] = hjelp;
            }
        }
    }
    for (int i = 0; i <= 19; i++) {
        System.out.println(randomArray[i]);
    }
}

How do I make my bubble sort work properly?


Updated on 13-Nov-2014

Can you answer this question?


Answer

1 Answers

Liked By