甚至在奇数之前,使用递归按升序排列

时间:2018-04-01 20:51:54

标签: java sorting recursion mergesort

我需要能够有一个整数数组,它们在奇数整数之前放置整数,然后按升序对数组进行排序。例如,给定数组[1,2,3,4,5],最终产品应该看起来像[2,4,1,3,5],因为你可以看到数组在赔率之前都有两个均衡然后放置它们按升序排列。

正如您在代码中看到的那样,我正在使用数组[10,9,8,7,6,5,4,3,2,1]。我得到的输出是[1,2,3,4,5,6,7,8,9,10],但这是不正确的 - 它应该是[2,4,6,8,10,1,3] ,5,7,9]。我不能使用任何内置函数或库。以下是我到目前为止的情况:

public class EBOMain {

static int[] Array  = { 10,9,8,7,6,5,4,3,2,1};
static int n;

public static void main(String[] args) {


    System.out.print("Array before sort: "  );
    for (int i = 0; i < Array.length; i++)
        System.out.print(Array[i] +" ");
    n = Array.length;
    EvenBeforeOdd(Array, n);

    int[] Array2 = new int[Array.length];
    MergeSort(Array2, 0,Array.length - 1);

    System.out.print("\nArray after sort: "  );
    for (int i = 0; i < Array.length; i++)
        System.out.print(Array[i] +" ");        
}

public static void EvenBeforeOdd(int []Array,int n){
      if (n==0)
        return;
    else if(Array[n-1]%2==0) {
        for(int i=0;i<n-1;i++) {
            if(Array[i]%2!=0) {      
                int temp = Array[i];
                Array[i]= Array[n-1];
                Array[n-1] = temp;

                EvenBeforeOdd(Array,n-1);
            }
        }
    }
    else
        EvenBeforeOdd(Array,n-1);
 }

 static void MergeSort(int[] Combine, int LowerIndex, int UpperIndex) {

    if (LowerIndex == UpperIndex){
           return;

    }

    else {    // locate Pivot
           int Pivot = (LowerIndex + UpperIndex) / 2;
           MergeSort(Combine, LowerIndex, Pivot);
           MergeSort(Combine, Pivot + 1, UpperIndex);
           Merge(Combine, LowerIndex, Pivot + 1, UpperIndex);
    }

 }



    static void Merge(int[] Array2, int Small, int Large, int UpperIndex) {

        int Pivot = Large - 1;
        int LowerIndex = Small;
        int n = UpperIndex - LowerIndex + 1;
        int i = 0;

        while (Small <= Pivot && Large <= UpperIndex)
               if (Array[Small] < Array[Large])
                     Array2[i++] = Array[Small++];
               else
                     Array2[i++] = Array[Large++];

        while (Small <= Pivot)
               Array2[i++] = Array[Small++];

        while (Large <= UpperIndex)
               Array2[i++] = Array[Large++];

        for (i = 0; i < n; i++)
               Array[LowerIndex + i] = Array2[i];

 }







}

2 个答案:

答案 0 :(得分:1)

您只需要两次调用MergeSort。确定最后一个偶数的位置,在偶数上调用MergeSort,然后在几率上调用。如果没有偶数(这里不包括),应该进行边界检查:

EvenBeforeOdd(Array, n);
int lastEven = 0;
for (int i=0; i<Array.length; i++) {
    if (Array[i] % 2 == 0)
        lastEven = i;
}
int[] Array2 = new int[Array.length];
MergeSort(Array2, 0, lastEven);
MergeSort(Array2, lastEven+1, Array.length - 1);

答案 1 :(得分:0)

以下是此问题的可能解决方案之一。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

class Main {

    private static final int[] array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

    public static void main(String[] args) {
        List<Integer> odd = new ArrayList<>();
        List<Integer> even = new ArrayList<>();
        for (int number : array) {
            if (number % 2 != 0)
                odd.add(number);
            else
                even.add(number);
        }

        Collections.sort(odd);
        Collections.sort(even);
        List<Integer> evenBeforeOdd = new ArrayList<>();
        Stream.of(even, odd).forEach(evenBeforeOdd::addAll);

        evenBeforeOdd.forEach(System.out::println);  
        // 2,4,6,8,10,1,3,5,7,9
    }
}