【问题标题】:How can to code sorting numbers (ascending/descending) via bubble sorting?如何通过冒泡排序编码排序数字(升序/降序)?
【发布时间】:2014-11-13 06:49:32
【问题描述】:

我需要关于使用冒泡排序和升序或降序排序的帮助:(

          int[] number = {12, 5, 6, 14, 18};    

      int[] number = new int[5];
      String[] mark = new String[10];
      String evenOrOdd = "";
      String output = "";

      JTextArea textArea = new JTextArea(12,30);


      for(int i = 0; i < number.length; i++) {
        number[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number"));

        if (number[i] % 2 == 0) {
            evenOrOdd = "even";
        }
        else {
            evenOrOdd = "odd  ";
        }

        mark[i] = "";
        for(int j = 0; j < number[i]; j++) {
            mark[i] = mark[i] + "*"; 
        }

        output = output + number[i] + "\t"; 
        output = output + evenOrOdd + "\t";
        output = output + mark[i] + "\n";
      } 

      textArea.setText("numbers\ttype\tgraph\n" + output);
      JOptionPane.showMessageDialog(null, 
                        textArea,
                                   "OUTPUT",
                                   JOptionPane.INFORMATION_MESSAGE);    
      System.exit(0);   
    }
}

}

代码缺少冒泡排序,我不知道把它放在哪里。 有人可以帮帮我吗?它不需要用户输入任何东西,

【问题讨论】:

  • 此代码无法编译。一方面,number 被声明了两次。你到底想要什么“冒泡排序”?你检查Wikipedia了吗?
  • 我已经对上升进行了编码。但我需要分析下降部分
  • @Elliot Frisch } public static void bubbleSort(int[] numero) { int n = numero.length;国际温度 = 0; for(int i=0; i numero[j]){ temp = numero[j -1];数字[j-1] = 数字[j];数字[j] = 温度; } } } }
  • @user3414251 您可能应该对原始问题文本进行更改和澄清,而不是在评论中。

标签: java sorting numbers descendant


【解决方案1】:

您支持升序和降序的方式是将Comparator 传递给您的sort() 方法并使用它来测试元素比较的结果,例如,

public static void bubbleSort(int[] numero, Comparator<Integer> comp) {
    int n = numero.length;
    int temp = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {
            if (comp.compare(numero[j - 1], numero[j]) > 0) {
                temp = numero[j - 1];
                numero[j - 1] = numero[j];
                numero[j] = temp;
            }
        }
    }
}

升序是Comparable 的默认行为,例如Integer。所以我们可以委派compareTo()like,

private static Comparator<Integer> ascending = new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);
    }
};

那么降序就是升序的逆向,所以委托和逆向一样

private static Comparator<Integer> descending = new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return -ascending.compare(o1, o2);
    }
};

然后测试一下

public static void main(String arg[]) {
    int[] arr = { 10, 30, 20 };
    System.out.println(Arrays.toString(arr));
    bubbleSort(arr, ascending);
    System.out.println("Ascending: " + Arrays.toString(arr));
    bubbleSort(arr, descending);
    System.out.println("Descending: " + Arrays.toString(arr));
}

输出是

[10, 30, 20]
Ascending: [10, 20, 30]
Descending: [30, 20, 10]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多