我编写了一个选择排序方法来降序数组的数字。该方法的 header 为:
public static void selectionSort(int[] num) { ... }
在主类中,我提示用户输入数组的大小和数组内的数字,但是我遇到了编译器错误,除了我如何在主类中使用数组编写方法之外,它并没有出现错误.
int sorted = selectionSort(arrayNum[arraySize]);
System.out.println("Sorted numbers of the array are: " + sorted);
我知道存在问题,因为int排序与数组int[] arrayNum不同,并且我的使用有问题方法。
在该方法中主类的数组的正确写入方法是什么?
最佳答案
此online compiler ide中的工作代码.
基本问题是,您可能实现或从书中获取的选择排序植入是以排序方法修改输入数组的典型方式植入的。 所以只要传递数组,数组就不会包含排序后的元素。 注意:在示例中,我刚刚使用了现有的升序排序方法。通过您的实现修复该方法。
int[] num = { 4, 8,2,1,6};
selectionSort(num);
System.out.println(Arrays.toString(num));
https://www.codiva.io/p/be4128fd-3a65-4b1a-8bb0-721fa5107369
关于java - 降序选择排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38235186/