-1
public class App {
    public static void main( String[] args ) {
        int[] set = {0,0,0,0,0,0,0};

        for(int i = 0; i < 7; i++) {
            boolean unique = false;
            while (!unique) {
                int j = 0;
                try {
                    j = JRandom.randInt(1, 45);
                }
                catch (ParamterException e) {
                    e.printStackTrace();
                }

                unique = true;
                for(int k = 0 ; k < 7; k++) {
                    if (j==set [k]) { 
                        unique = false;
                        break;
                    }
                }

                if (unique) {
                    set [i] = j;
                }
            }
        }


        for (int i= 0; i < 6; i++) {
            if (i == 5) {
                System.out.println(set[i]);
            }
            else {
                System.out.print(set[i]+", ");
            }
        }

        System.out.println("Bonus Ball = " + set[6]);
    }
}

I was wondering how I would implement bubble-sort into this code. It works on my machine and produces 6 random numbers + a bonus ball.

2, 34, 25, 14, 39, 13

Bonus Ball = 30

I was aiming to make it print so the numbers are ascending so that it would be like the lotto, the idea anyway.

That's all, thanks.

jsosnowski
  • 1,560
  • 3
  • 26
  • 56
Andy
  • 1
  • 2

3 Answers3

1

Can make like this:

        for (int i = set.length-1; i >= 0; i --) {   

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

            if (set[j] > set[j + 1]) {   
                 int aux = set[j];        
                 set[j] = set[j + 1];  
                 set[j + 1] = aux;        
            }           
        }       
    }

The initial loop, in this way also helps, not to waste resources comparing indexes that have already been sorted.

If the use of bubble sort algorithm is not mandatory, just use the Array.sort method already mentioned.

  • Worked perfectly, thanks Nuno! If you could be super helpful could you layout how this works in layman terms, thanks. – Andy Aug 10 '15 at 12:13
  • The inner for loop is the one doing the comparison between index[i] and index[i+1], so it starts comparing in your array called set the index set[0] with set[1], then if set[1] is bigger then set[0] he stores the set[0] value in a auxiliary variable, assigns set[1] value to set[0] and assigns the former value of set[0] which was stored in aux to set[1], then he will do this trough out the entire array comparing each time set[i] with set[i+1], until the biggest number is stored at the final index of the array. – Nuno Henriques Aug 11 '15 at 08:43
  • Once he does this, the outer for loop, is decremented, so that the program wastes no time comparing the values already sorted. Then it starts all over gain till outer loop index is [0]. Sorry had to break comment in 2 cause of too many letters :) – Nuno Henriques Aug 11 '15 at 08:46
  • Thanks, super helpful. – Andy Aug 11 '15 at 12:06
0
for (i = 0 ; i < set.length() -2; i ++){ 
   for(j = i+1 ; j< set.length()-1 ; j++){ 
   if(set[i]<set[j]){
     int dummy = set[i];
     set[i] = set[j];
     set[j] = dummy;
     }
   }
}

The above bubble sort is sorting only the 6 generated numbers and your Bonus ball is still present at set[6]

So now rest is your printing function works.

SSH
  • 1,609
  • 2
  • 22
  • 42
  • Where would I need to place this in the code? Thanks! :) – Andy Aug 10 '15 at 12:03
  • before your last for loop, which is printing , so now you need to first sort it, also accept the answer if it works for you... ;) – SSH Aug 10 '15 at 12:04
0

Bubble sort set array :-

int temp = 0;
for(int i = 0;i<set.length-1;i++)
{
    for(int j = 0;j<set.length-1;j++)
    {
         if(set[j]>set[j+1])
         {
             temp = set[j];
             set[j] = set[j+1];
             set[j+1] = temp;
         }
    }
}
//Your set array is sorted using bubble sort at this point.
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51