【问题标题】:Java- Sorting int arrayJava-排序int数组
【发布时间】:2016-11-01 22:20:39
【问题描述】:

我正在尝试使用此方法按升序对整数数组进行排序。但我的 for 循环只运行一次。

public void sortArray()
{
   boolean sorted = false;

   while(sorted == false)
   {
       int temp;
       for(int i = 0; i < inArray.length - 1; i++)
       {
          if(inArray[i] > inArray[i + 1])
          {
             temp = inArray[i];
             inArray[i] = inArray[i + 1];
             anArray[i + 1] = temp;
          }
       }
       sorted = true;
   }
}

我知道这与我处理布尔标志的方式有关,但我不确定如何修复它。任何建议,将不胜感激。提前致谢。

【问题讨论】:

  • = 是赋值,== 是相等。这就是为什么你应该总是写while (!sorted)。它可以保护您免受这种错字的影响。
  • “但是我的 for 循环只运行了一次。” 不,你当前的 while 循环不会执行循环体根本 i>,因为上面提到的 azurefrog。如果你解决了这个问题,它只会运行一次,因为你在循环结束时无条件地设置了sorted = true
  • @azurefrog 感谢您指出这一点,我实际上在我的实际代码中将其命名为 ==,但在此处输入时搞砸了。
  • @Bluasul:你为什么要重新输入它?这就是复制和粘贴的用途。
  • @T.J.Crowder 我觉得再次输入它会更仔细地审查它,并且可能会在发布问题之前找到解决方案。结果是相反的,因为我最终把它弄得更糟了,但是哦,好吧。

标签: java arrays sorting bubble-sort


【解决方案1】:

这里有多个问题:

  1. while (sorted = false)sorted 设置为false,然后测试结果值false,这意味着您永远不会进入循环体(根据您的问题)。

  2. 如果你解决了这个问题,你的代码将只运行while循环体一次(从而使数组尚未排序),因为你有sorted = true作为一个unconditional 循环体末尾的语句。

您需要有一个标志,假设数组已排序,然后如果您发现没有的证据则将其清除,例如:

public void sortArray()
{
   boolean sorted;

   do
   {
       sorted = true;  // Assume it's sorted
       int temp;
       for(int i = 0; i < inArray.length - 1; i++)
       {
          if(inArray[i] > inArray[i + 1])
          {
             temp = inArray[i];
             inArray[i] = inArray[i + 1];
             anArray[i + 1] = temp;
             sorted = false; // We changed something, so assume we need to do another pass
          }
       }
   }
   while (!sorted);
}

旁注:这只是一个样式问题,但通常最好尽可能缩小变量范围。 temp 没有必要在 for 循环之外,甚至在 if 块之外,将它移到 if 块内

public void sortArray()
{
   boolean sorted;

   do
   {
       sorted = true;  // Assume it's sorted
       for(int i = 0; i < inArray.length - 1; i++)
       {
          if(inArray[i] > inArray[i + 1])
          {
             int temp = inArray[i];
             inArray[i] = inArray[i + 1];
             anArray[i + 1] = temp;
             sorted = false; // We changed something, so assume we need to do another pass
          }
       }
   }
   while (!sorted);
}

【讨论】:

    【解决方案2】:

    您当前在循环结束时始终将 sorted 设置为 true。当然,只有在实际上没有发生改组的情况下,它才应该是正确的。

    实现这一点的一种方法是在 while 循环开始时将 sorted 设置为 true,并在您检测到数组尚未排序并进行元素切换时将其设置为 false:

    public void sortArray()
    {
       boolean sorted = false;
    
       while(!sorted)
       {
           sorted = true;
           int temp;
           for(int i = 0; i < inArray.length - 1; i++)
           {
              if(inArray[i] > inArray[i + 1])
              {
                 sorted = false; // array is not yet sorted
                 temp = inArray[i];
                 inArray[i] = inArray[i + 1];
                 anArray[i + 1] = temp;
              }
           }
    
       }
    }
    

    【讨论】:

      【解决方案3】:

      我的方法不包含 java 集合


      public class Main {
      public static void main(String[] args) {
          /** By Boris Elkin 21.09.2018 в 22.59 MSK You can input any digits, sorting was made without collections on purpose.
           **/
      
          int[] a1=new int[]{1,245623,3,3,3,3454,6,8123,234,123123,797897};
          int[] a2=new int[]{234234, 33,4234,5,646456,9,78};
          int[] a3;
          a3= collide(a1, a2);
          a3=sort(a3);
          checkArray(a3);
      }
      public static int[] collide(int[]a, int[]b){
          int breakpoint=0;
          int size=a.length+b.length;
          int[]c=new int[size];
          for(int i=0;i<a.length;i++){
              c[i]=a[i];
              breakpoint=i;
          }
          for(int i=breakpoint+1,j=0;j<b.length;i++, j++){
              c[i]=b[j];
          }
          return c;
      }
      public static int[] sort(int a[]){
          boolean engine=true;
          while(engine) {
              for(int i=0;i<a.length;i++){
                  int temp, temp2;
                  if ((i + 1 < a.length) && (a[i] > a[i + 1])) {
                      temp = a[i];
                      temp2 = a[i + 1];
                      a[i + 1] = temp;
                      a[i] = temp2;
                  }
              }
              if(checkThreadLogistic(a)){
                  engine=false;
              }
          }
          return a;
      }
      
      private static boolean checkThreadLogistic(int[] a) {
          return checkCertainElement(a);
      }
      
      private static boolean checkCertainElement(int[] a) {
          for(int i=0;i<a.length;i++){
              if(i>1){
                  for(int j=a.length;j>i;j--){
                      if(j<a.length) if(a[i]>a[j])return false;
                  }
              }
          }
          return true;
      }
      
      public static void checkArray(int[]array){
          for (int anArray : array) {
              System.out.println(anArray + "");
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        • 2021-10-03
        • 2017-02-17
        • 2023-01-20
        • 2012-04-05
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多