如何仅对整数数组中的奇数进行排序,保留偶数在其原始位置?

huangapple go评论80阅读模式
英文:

How do you only order odd numbers in an int array and leave even numbers in their original position?

问题

基本上,有一个在线挑战,你需要对数组中的奇数进行升序排序,同时忽略偶数。例如,[3,1,4,2,3,6] 应该变成 [1,3,4,2,3,6]。我已经编写了一个算法来对数字进行排序,但输出是错误的。

import java.util.Arrays;

public class Kata {
    public static int[] sortArray(int[] array) {

        int n;
        int temp = 0;
        if (array.length < 2){
            System.out.println(Arrays.toString(array));
            return array;
        }

        for (n = 1; n < array.length; n++) { // 遍历数组
            if (array[n] % 2 != 0 && array[n] > array[n-1] && array[n-1] % 2 != 0){ // 如果 n 是奇数且比 n-1 大,而且 n-1 也是奇数
                temp = array[n];
                array[n] = array[n-1];
                array[n-1] = temp;
            }
            else if (array[n] % 2 == 0 && array[n] < array[n-1]){
                array[n] = array[n];
            }
            else if (array[n] % 2 != 0 && array[n] > array[n-1]){
                array[n] = array[n];
            }
        }
        System.out.println(Arrays.toString(array));
        return array;
    }
}

驱动代码

public class Driver {
    public static void main(String[] args) {

       Kata.sortArray(new int[]{3,1,11,2,9});
      
    }
}

对于上面的数组,期望输出应该是:[1,3,9,2,11],但实际输出却是 [3, 11, 1, 2, 9]。我已经盯着这个问题看了很久,完全想不通。非常感谢阅读!

英文:

Basically, there is this challenge online where you have to order the odd numbers in an array in ascending order, while also ignoring the even numbers. So for example, [3,1,4,2,3,6] would be -> [1,3,4,2,3,6]. I've written an algorithm to sort the numbers, but the output is wrong.

import java.util.Arrays;

public class Kata {
    public static int[] sortArray(int[] array) {

        int n;
        int temp = 0;
        if (array.length &lt; 2){
            System.out.println(Arrays.toString(array));
            return array;
        }

        for (n = 1; n &lt; array.length; n++) { // iterates through array
            if (array[n] % 2 != 0 &amp;&amp; array[n] &gt; array[n-1] &amp;&amp; array[n-1] % 2 != 0){ // if n is odd and smaller than n-1 and n-1 is odd
                temp = array[n];
                array[n] = array[n-1];
                array[n-1] = temp;

            }
            else if (array[n] % 2 == 0 &amp;&amp; array[n] &lt; array[n-1]){
                array[n] = array[n];
            }
            else if (array[n] % 2 != 0 &amp;&amp; array[n] &gt; array[n-1]){
                array[n] = array[n];
            }

        }
        System.out.println(Arrays.toString(array));
        return array;

    }
}

Driver code

public class Driver {
    public static void main(String[] args) {

       Kata.sortArray(new int[]{3,1,11,2,9});
      
    }
}

for the array above, the output should be: [1,3,9,2,11] but instead it's [3, 11, 1, 2, 9]. I've been staring at this for ages and I'm stumped, honestly. Thanks a lot for reading!

答案1

得分: 2

我使用了一个内循环来查找下一个要排序的奇数而且我使用了额外的外循环因为通常排序需要两个循环

import java.util.Arrays;

public class Kata {
    public static int[] sortArray(int[] array) {
        int i;
        int n;
        int m;
        int temp = 0;
        if (array.length < 2) {
            System.out.println(Arrays.toString(array));
            return array;
        }

        for (i = 0; i < array.length; i++) {
            for (n = 0; n < array.length; n++) {
                if (array[n] % 2 != 0) {
                    for (m = n + 1; m < array.length; m++) {
                        if (array[m] % 2 != 0) {
                            if (array[n] > array[m]) {
                                temp = array[n];
                                array[n] = array[m];
                                array[m] = temp;
                            }
                            break;
                        }
                    }
                }
            }
        }
        System.out.println(Arrays.toString(array));
        return array;
    }
}
英文:

I used an inner loop to find the next odd number to sort. And I used an additional outer loop because normally sorting needs two loops.

import java.util.Arrays;

public class Kata {
public static int[] sortArray(int[] array) {
	int i;
	int n;
	int m;
	int temp = 0;
	if (array.length &lt; 2) {
		System.out.println(Arrays.toString(array));
		return array;
	}

	for (i = 0; i &lt; array.length; i++) {
		for (n = 0; n &lt; array.length; n++) {
			if (array[n] % 2 != 0) {
				for (m = n + 1; m &lt; array.length; m++) {
					if (array[m] % 2 != 0) {
						if (array[n] &gt; array[m]) {
							temp = array[n];
							array[n] = array[m];
							array[m] = temp;
						}
						break;
					}
				}
			}
		}
	}
	System.out.println(Arrays.toString(array));
	return array;
}
}

huangapple
  • 本文由 发表于 2020年9月15日 21:49:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63903362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定