【问题标题】:Sort array using if condition in java在java中使用if条件对数组进行排序
【发布时间】:2017-01-25 21:51:38
【问题描述】:

下面的程序是在 java 中,它假设按升序对数组元素进行排序,但它不是这样运行的。请尝试下面的程序并解释为什么它没有将某些值存储在“第二个”数组中。第二个数组中的一些值存储为 0。我无法计算。请解决这个程序并让我知道。谢谢。

public static void main(String[] args) {
    int first[]={9,8,2,6,3};
    int second[]=new int[5];
    for(int i=0;i<first.length;i++){
        int count=0;
        for(int j=0;j<second.length;j++){
            if(first[i]<first[j]){
                count++;
            }
        }      
        if(count == 0){
            second[4]=first[i];
        }
        if(count == 1){
            second[3]=first[i];
        }
        if(count == 2){
            second[2]=first[i];
        }
        if(count == 3){
            second[1]=first[i];
        }
        if(count == 4){
            second[0]=first[i];
        }
        System.out.print(second[i]);
    }
}

【问题讨论】:

  • 如果你只想对一个 int 数组进行排序。有什么原因,为什么你不使用 Arrays.sort()?
  • 你知道如何调试 Naveen 吗??
  • 如果您想在不使用 Arrays.sort() 的情况下对数组进行排序,为什么不查找诸如合并排序、快速排序甚至冒泡排序之类的排序算法?
  • 我可能不太了解,但这是否是一个有效的排序算法?
  • 您的问题是,您覆盖了second 中的值。因为count 可以多次使用相同的值。这可以通过调试轻松找到。 stackoverflow.com/questions/18977397/…

标签: java arrays sorting


【解决方案1】:

这里的代码可以满足你的要求:

public class SortIt{
    public static void main(String[] args){
        int[] arr = {9,8,2,6,3};
        boolean a = true;
        int index = 0;
        int arrBuffer;
        int check = 0;
        while(a){
            if(index + 1 != arr.length){
                if(arr[index] > arr[index+1]){
                    arrBuffer = arr[index+1];
                    arr[index+1] = arr[index];
                    arr[index] = arrBuffer;
                    index += 1;
                    check = 0;
                }
                else if(arr[index] <= arr[index+1]){
                    index += 1;
                    check += 1;
                }
                if(check == arr.length){a = false;}
            }
            else
                index = 0;
        }
        System.out.print("[");
        for(int i = 0; i + 1 < arr.length; i++){
            System.out.print("'" + arr[i] + "', " );
        }
        System.out.println("'" + arr[arr.length-1] + "']");
    }
}

输入:

9,8,2,6,3

输出:

['2', '3', '6', '8', '9']

【讨论】:

    【解决方案2】:
    public static void main(String[] args) {
        int first[]={9,8,2,6,3};
        int second[]=new int[5];
        for(int i=0;i<first.length;i++){
            int count=0;
            for(int j=0;j<second.length;j++){
                if(first[i]<first[j]){
                    count++;
                }
            }      
            if(count == 0){
                second[4]=first[i];
            }
            if(count == 1){
                second[3]=first[i];
            }
            if(count == 2){
                second[2]=first[i];
            }
            if(count == 3){
                second[1]=first[i];
            }
            if(count == 4){
                second[0]=first[i];
            }
            //System.out.print(second[i]);
        }
        for(int a:second){
            System.out.println(a);
        }
    }
    

    【讨论】:

      【解决方案3】:

      您在循环内打印数组,这就是为什么某些值被打印为零的原因。她是修改后的代码。

      public static void main(String[] args) {
          int first[]={9,8,2,6,3};
          int second[]=new int[5];
          for(int i=0;i<first.length;i++){
              int count=0;
              for(int j=0;j<second.length;j++){
                  if(first[i]<first[j]){
                      count++;
                  }
              }      
              if(count == 0){
                  second[4]=first[i];
              }
              if(count == 1){
                  second[3]=first[i];
              }
              if(count == 2){
                  second[2]=first[i];
              }
              if(count == 3){
                  second[1]=first[i];
              }
              if(count == 4){
                  second[0]=first[i];
              }
          }
      
          System.out.println(new Gson().toJson(second));
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-02
        • 2021-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        • 2021-02-23
        • 2018-03-22
        • 2021-06-11
        相关资源
        最近更新 更多