mardi 31 mai 2016

Android : how to generate unique random numbers in an array and perform bubble sort

The following is my code but it doesn't generate unique random numbers

Random rand = new Random();

            int[] array = new int[n];

            for (int i = 0; i < n; i++){

                array[i] = rand.nextInt(n)+1;

              }




            for (int i = 0; i < ( n - 1 ); i++)
            {
                for (int j = 0; j < n - i - 1; j++) {
                    if (array[j] > array[j+1])        {
                        int temp = array[j];
                        array[j]   = array[j+1];
                        array[j+1] = temp;
                    }
                }
            }




Aucun commentaire:

Enregistrer un commentaire