Reputation: 1
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.
Upvotes: 0
Views: 527
Reputation: 1078
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] + " ");
}
}
}
Upvotes: 0
Reputation: 127
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;
}
Upvotes: 1