Java- 排序 int 数组

Java- Sorting int array

我正在尝试使用此方法按升序对整数数组进行排序。但是我的 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;
   }
}

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

您目前在循环结束时始终将 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;
          }
       }

   }
}

这里有多个问题:

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

  2. 如果你解决了这个问题,你的代码只会 运行 while 循环体 一次 (从而使数组不排序yet),因为在循环体的末尾有 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);
}

我的方法不包含 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 + "");
    }
}
}