简体   繁体   English

你如何按降序和升序对奇数和偶数数组进行排序?

[英]How do you sort odd and even array numbers in descending and ascending order?

I need help to sort numbers in an array in ascending and descending order.我需要帮助按升序和降序对数组中的数字进行排序。 Even numbers should be ascending and odd numbers descending.偶数应该升序,奇数应该降序。

I have managed to sort the number in ascending order but want to do the opposite for the odd numbers.我已经设法按升序对数字进行排序,但想对奇数进行相反的操作。

Actual Results: Both odd and even numbers ascending实际结果:奇数和偶数都升序

结果

Expected Results: Even numbers ascending and odd numbers descending预期结果:偶数递增,奇数递减

结果

System.out.println("\n" + "random numbers generated:");
System.out.println(Arrays.toString(arrayList).replace("[", "").replace("]", "").replace(",", ""));
for (int i = 0; i < arrayList.length; i++) {
    for (int j = i+1; j < arrayList.length; j++) {
        if(arrayList[i] > arrayList[j]) {
            temporaryArray = arrayList[i];
            arrayList[i] = arrayList[j];
            arrayList[j] = temporaryArray;
        }
    }
}
System.out.println("\n" + "random numbers arranged:");

int[] arrayTwo = Arrays.copyOf(arrayList, arrayList.length);

for (int i = 0; i < arrayList.length; i++) {
    if(arrayTwo[i]%2!=0) {
        System.out.print(arrayTwo[i] + " ");
    }
}

System.out.print("| ");

for (int i = 0; i < arrayList.length; i++) {
    if(arrayTwo[i]%2==0) {
        System.out.print(arrayTwo[i] + " ");
    }
}

How can I reverse array for odd numbers?如何反转奇数数组?

I would recommend that you:我建议您:

    1. sort all numbers in ascending order,按升序排列所有数字,
    1. extract the odd numbers to a second array and将奇数提取到第二个数组中,然后
    1. reverse this new array.反转这个新数组。 Manipulating two different orderings in a same array would prove much more complicated.在同一个数组中操作两个不同的顺序会复杂得多。 On a side note, if you don't have a requirement not to, I'd recommend using the Arrays library's Arrays.sort() to simplify your code.另外,如果您没有要求,我建议您使用Arrays库的Arrays.sort()来简化您的代码。 It is very readable and has lower complexity of O(N log N).它非常可读,并且具有较低的 O(N log N) 复杂度。

What you can do is write a recursive function like so:你可以做的是像这样写一个递归的 function :

public static void printOddReversed(int[] array, int index) {
    if (index == array.length)
        return;

    printOddReversed(array, index + 1);

    if (array[index] % 2 != 0) {
        System.out.print(array[index] + " ");
    }
}

and then instead of the second loop, call it like so:然后代替第二个循环,这样称呼它:

printOddReversed(arrayTwo, 0);

A simple way to do that is by using a special comparator:一个简单的方法是使用一个特殊的比较器:

        if (a%2 == 0) {
            if (b%2 == 0) {
                return Integer.compare(a,b);
            } else {
                return -1;
            }
        } else {
            if (b%2 == 0) {
                return 1;
            } else {
                return Integer.compare(b, a);
            }
        }

UPDATE: Example更新:示例

    int[] arrayList = {959, 321, 658, 3, 506, 165, 560, 582, 199, 533, 178};
    for (int i = 0; i < arrayList.length; i++) {
        for (int j = i+1; j < arrayList.length; j++) {
            if(compare(arrayList[i], arrayList[j]) > 0) {
                int temporaryArray = arrayList[i];
                arrayList[i] = arrayList[j];
                arrayList[j] = temporaryArray;
            }
        }
    }

    System.out.println(Arrays.toString(arrayList));

... ...

private static int compare(int a, int b) {
    if (a%2 == 0) {
        if (b%2 == 0) {
            return Integer.compare(a,b);
        } else {
            return -1;
        }
    } else {
        if (b%2 == 0) {
            return 1;
        } else {
            return Integer.compare(b, a);
        }
    }
}

I managed to solve this by just reversing我设法通过倒车解决了这个问题

for (int i = arrayList.length-1;i>=0;i--) {
    if(arrayTwo[i]%2==0) {
        System.out.print(arrayTwo[i] + " ");

暂无
暂无

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

相关问题 按升序对所有偶数进行排序,然后按集合中的降序对所有奇数进行排序 - Sort all even numbers in ascending order and then sort all odd numbers in descending order in a collection 如何对数组进行二进制搜索,即偶数索引上的数字是升序的,奇数索引上的数字是降序的 - how to do binary search on array which is the numbers on even indexes are ascending and the numbers on odd indexes are descending 如何在不使用集合的情况下按升序打印偶数和按降序打印奇数 - How to print even numbers in ascending order and odd numbers in descending order without using collection 我只想按升序对数组中的奇数进行排序,并将偶数留在它们在 java 中的原始位置 - I want to sort only odd numbers in an array in ascending order and leaving even numbers at their orignal place in java 如何从for循环中按升序对数字排序? - How do you sort numbers in ascending order from a for loop? 如何只对 int 数组中的奇数进行排序,而将偶数保留在其原始位置? - How do you only order odd numbers in an int array and leave even numbers in their original position? 按升序和降序对数组的 LinkedHashmap 进行排序 - Java - Sort LinkedHashmap of Array in ascending and descending order - Java 按顺序排列偶数和奇数 - Sort even and odd numbers in order 对偶数和奇数数组中的奇数进行排序 - Sort odd numbers in Array of even and odd numbers 如何使用一种方法以升序和降序输入数组? - How do I use a method to input an array in ascending and descending order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM