Java冒泡排序算法实现

时间:2016-12-11 20:13:38

标签: java algorithm sorting bubble-sort

我正在尝试将冒泡排序算法实现到我的代码中,以使输出按升序显示。下面是我的代码,接下来是我在JGrasp中收到的错误。任何建议或指示将不胜感激。

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] + " ");
                        }
                     } 
               }
            }
      }


}

我收到的错误是:

----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.

2 个答案:

答案 0 :(得分:1)

这是实现冒泡排序的方法。我刚刚编写了返回数组的方法。 这将按升序排序您的数字:

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;
   }

答案 1 :(得分:0)

我认为你想要生成15个随机数并按加入顺序打印 这是代码。

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] + " ");
            }
      }
}