0

I am trying to implement the Bubble Sort Algorithm into my code to have the output appear in ascending order. Here is my code below followed by the errors I receive in JGrasp. Any advice or pointers would be appreciated.

import java.util.*;

public class RandomArray
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.println("Please enter the number 15 to generate 15 random numbers!");
      int randomIn = input.nextInt();
      int[] randomNumbers = new int[randomIn];
      if(randomIn != 15)
      {
         System.out.println("The number you entered was not 15. Please run the program again and enter 15...");
      }
      else if(randomIn == 15)
      {
         for(int x = 0; x < randomNumbers.length; ++x)
         {
            randomNumbers[x] = (int) (Math.random()*50);
            System.out.println("Your randomly generated numbers are: " + randomNumbers[x]);
         }
      }

   }
   public static void bubbleSort(int[] randomNumbers)
      {
         int n = randomNumbs.length;
         int temp = 0;
         for(int i = 0; i < n; i++)
            {
               for(int j = 1; j < (n - 1); j++)
               {
                  if(randomNumbers[j-1] > randomNumbers[j])
                     {
                        temp = randomNumbers[j - 1];
                        randomNumbers[j - 1] = randomNumbers[j];
                        randomNumbers[j] = temp;
                        for(int i=0); i<randomNumbers.length; i++)
                        {
                           System.out.print(randomNumbers[i] + " ");
                        }
                     } 
               }
            }
      }


}

The errors I receive are:

----jGRASP exec: javac -g RandomArray.java
RandomArray.java:38: error: ';' expected
                        for(int i=0); i<randomNumbers.length; i++)
                                   ^
RandomArray.java:38: error: not a statement
                        for(int i=0); i<randomNumbers.length; i++)
                                       ^
RandomArray.java:38: error: ')' expected
                        for(int i=0); i<randomNumbers.length; i++)
                                                            ^
RandomArray.java:38: error: ';' expected
                        for(int i=0); i<randomNumbers.length; i++)
                                                                 ^
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
1
  • 3
    Check again, what the syntax of a for loop is... Commented Dec 11, 2016 at 20:15

2 Answers 2

1

This is how you implement a bubble sort. I just wrote the method that return an array. This will order your numbers in ascending order:

public int []bubbleSort(int[] arr) {
      int size = arr.length;
      for (int pass = 1; pass < size; pass++) {
         for (int i = 0; i < size-pass; i++) {
            if (arr[i] > arr[i+1]) {
               int temp = arr[i];
               arr[i] = arr[i+1];
               arr[i+1] = temp;
            }
         }

      }
      return arr;
   }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. That was what I was looking for.
@DontrellJohnson Please, if you solved the problem mark the correct answer.
As you said you wanted the numbers to sorted in ascending order, instead of implementing bubble sort you could have used the Array.sort() method.
0

I think you want to do generate 15 random numbers and and print it in acceding order
Here is the code.

import java.util.*;
public class RandomArray
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.println("Please enter the number 15 to generate 15 random numbers!");
      int randomIn = input.nextInt();
      int[] randomNumbers = new int[randomIn];
      if(randomIn != 15)
      {
         System.out.println("The number you entered was not 15. Please run the program again and enter 15...");
      }
      else if(randomIn == 15)
      {
         for(int x = 0; x < randomNumbers.length; ++x)
         {
            randomNumbers[x] = (int) (Math.random()*50);
            System.out.println("Your randomly generated numbers are: " + randomNumbers[x]);
         }
         bubbleSort(randomNumbers);
      }

   }
   public static void bubbleSort(int[] randomNumbers)
      {
         int n = randomNumbers.length;
         int temp = 0;
         for(int i = 0; i < n; i++)
            {
               for(int j = 1; j < (n - 1); j++)
               {
                  if(randomNumbers[j-1] > randomNumbers[j])
                     {
                        temp = randomNumbers[j - 1];
                        randomNumbers[j - 1] = randomNumbers[j];
                        randomNumbers[j] = temp;                    
                     } 
               }
            }
            for(int k=0; k<randomNumbers.length; k++)
            {
            System.out.print(randomNumbers[k] + " ");
            }
      }
}

2 Comments

Thanks for your help. I saw where I went wrong thanks to your help.
@DontrellJohnson If my answer helped you then upvote and accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.