vendredi 27 novembre 2015

How to sort random number in bubble sort

I'm trying to put run a program that would print 1000 random number and sorted them in bubble sorting but when I run this code, it'll print my 1000 random numbers but wont sort it out. Please help me understand.

import java.util.Random;
import java.util.Arrays;
public class JavaApplication3 {
    public static void main(String [] args){
    Random g = new Random();

    int [] number = new int [1000];

    System.out.print("Random Numbers:");
    for (int d = 0 ; d<number.length ; d++){
        int RandomG = g.nextInt(1000)+1;
        System.out.print(" " +RandomG);
        }

  System.out.print("\nSorted Numbers:"+Arrays.toString(BubbleSortAsceMethod(number)));

                     }
     public static int [] BubbleSortAsceMethod(int[] number){
    int temp;

    for(int i = 0 ; i < number.length-1 ; i++){
        for ( int j = 1 ; j < number.length-1 ; j++){
            if ( number[j-1] < number[j]){
                temp = number[j-1];
                number[j-1] = number[j];
                number[j] = temp;
            }
            }
                }
        return number;   
                }
}



Aucun commentaire:

Enregistrer un commentaire