简体   繁体   English

Java-排序int数组

[英]Java- Sorting int array

I'm trying to use this method to sort an integer array in ascending order. 我正在尝试使用此方法以升序对整数数组进行排序。 But my for loop runs through it only once. 但是我的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;
   }
}

I know it has to do with how I'm handling that boolean flag, but I'm not sure how to go about fixing it. 我知道这与我如何处理该布尔标志有关,但是我不确定如何解决它。 Any suggestions would be appreciated. 任何建议,将不胜感激。 Thanks in advance. 提前致谢。

You currently are setting your sorted to true allways at the end of the loop. 当前,您正在循环末尾将排序设置为true。 While of course it should only be true if actually no reshuffling took place. 当然,只有在实际上没有进行任何改组的情况下,它才应该是正确的。

One way to archieve this would be to set sorted to true at the start of your while loop, and set it to false when you detect that the array is not yet sorted and you do the switching of elements: 存档的一种方法是在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;
          }
       }

   }
}

There are multiple issues here: 这里有多个问题:

  1. while (sorted = false) sets sorted to false and then tests the resulting value false , meaning that you never enter the loop body at all (not once as per your question). while (sorted = false)sorted ,以false ,然后测试结果值false的,这意味着你根本就不会进入循环体(一次也没有按你的问题)。

  2. If you fix that, your code will only run the while loop body once (thus leaving the array not sorted yet), because you have sorted = true as an unconditional statement at the end of the loop body. 如果您对此进行了修复,则代码将只运行一次 while循环主体(因此不对数组进行排序),因为在循环主体的末尾将sorted = true作为无条件语句进行了处理。

You need to have a flag that assumes the array is sorted, and then is cleared if you find evidence it wasn't, something like: 您需要有一个标志,假定该数组已排序,然后如果发现不是,则清除该标志,例如:

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

Side note: This is just a style thing, but it's generally best to scope variables as narrowly as possible. 旁注:这只是一种样式,但是通常最好将变量的范围尽可能缩小。 There's no need for temp to be outside the for loop or even outside the if block, move it inside the if block 有没有需要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);
}

My method which contains no java collections 我的方法不包含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 + "");
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM