简体   繁体   English

Java:未排序的数组到降序的数组

[英]Java: Unsorted Array to Descending Sorted Array

I am a beginner learning java and I am trying to transfer the variables in an array to another array in descending order. 我是一个学习Java的初学者,我正尝试将数组中的变量按降序传输到另一个数组。 This is my code: 这是我的代码:

    int num[] = new int[6];
    int DecSort[] = new int[6];
    System.out.println("Input 6 numbers: ");
    for (int i = 0; i < 6; i++) {
        num[i] = Integer.parseInt(br.readLine());
    }
    //sorted array is descending
    int max = num[0];
    int maxpos = 0;
    for (int j = 0; j < 6; j++) {
        for (int i = 0; i < 6; i++) {
            if (num[i] > max) {
                max = num[i];
                maxpos = i;
            }
        }
        DecSort[j] = max;
        max = num[0];
        num[maxpos] = Integer.MIN_VALUE;
    }

    System.out.println("sorted array");
    for (int i = 0; i < 6; i++) {
        System.out.println(DecSort[i]);
    }

When I input 213, 45, 1234, 765, 23, 5 as the array, it outputs: sorted array 1234 765 213 213 213 213 当我输入213、45、1234、765、23、5作为数组时,它输出:排序后的数组1234765213213213213213

I am not sure where my mistake is, please help! 我不确定我的错误在哪里,请帮忙!

The problem is that you're initializing max to num[0] . 问题是您正在将max初始化为num[0] This means that any number in the array smaller than the first will never be found, because num[i] > max will always be false . 这意味着将永远找不到数组中小于第一个的任何数字,因为num[i] > max始终为false You could fix it by setting your initial value of max to Integer.MIN_VALUE . 您可以通过将max的初始值设置为Integer.MIN_VALUE来修复它。

However, are you aware that the Java Standard Library already has sort functionality. 但是,您知道Java标准库已经具有排序功能。 You could use that to sort you array in ascending order, and then just print it in reverse: 您可以使用它以升序对数组进行排序,然后以相反的方式打印它:

int num[] = new int[6];
System.out.println("Input 6 numbers: ");
for (int i = 0; i < 6; i++) {
    num[i] = Integer.parseInt(br.readLine());
}

Arrays.sort(num);

System.out.println("sorted array");
for (int i = 5; i >= 0; --i) {
    System.out.println(num[i]);
}

I'll leave you to work out how to turn an ascending array into a descending one if that's what you need. 如果需要的话,我将让您研究如何将升序数组转换为降序数组。 ;) ;)


Alternatively, using a NavigableSet , you can even get Java to sort it for you on the fly: 另外,使用NavigableSet ,您甚至可以获取Java对其进行即时排序:

NavigableSet<Integer> nums = new TreeSet<>();
System.out.println("Input 6 numbers: ");
for (int i = 0; i < 6; i++) {
    nums.add(Integer.valueOf(br.readLine()));
}

Integer[] sortedNums = nums.descendingSet().toArray(new Integer[6]);

I changed just a few your code: 我只更改了一些代码:

1 - Removed DecSort array beacause its not needed. 1-删除了DecSort数组,因为不需要它。

2 - Applied bubble sort descending algorithm. 2-应用气泡排序降序算法。

3 - Change de break condition to the for in order to be used for any size array. 3-将中断条件更改为for ,以便用于任何大小的数组。

Have a look: 看一看:

public void orderDescending() {
    int num[] = { 213, 45, 1234, 765, 23, 5 };
    // sorted array is descending
    int maxpos = 0;
    for (int j = 0; j < num.length; j++) {
        for (int i = j; i < num.length - 1; i++) {
            if (num[i] > num[j]) {
                int tempA = num[i];
                num[i] = num[j];
                num[j] = tempA;
            }
        }
    }

    System.out.println("sorted array");
    for (int i = 0; i < 6; i++) {
        System.out.println(num[i]);
    }
}

Just a few changes and works perfect! 只需进行一些更改即可完美工作!

Can create Comparator and that can be control your ascending and descending order. 可以创建比较器,并且可以控制您的升序和降序。

Comparator<Integer> comparator = new Comparator<Integer>() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    };

    // option 1
    Integer[] array = new Integer[] { 1, 24, 4, 4, 345 };
    Arrays.sort(array, comparator);

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

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