简体   繁体   English

在数组中重新排列偶数和奇数

[英]Rearrange even and odd in an array

I have written a program to place all even elements of the array to the left and odd ones to the right half of the array. 我编写了一个程序,将数组的所有偶数元素放在数组的左半边,将奇数元素放在数组的右半边。 The order of elements is not of concern. 元素的顺序无关紧要。 I was wondering if there is more efficient algorithm than this. 我想知道是否有比这更有效的算法。 My worst case complexity is O(n/2). 我最糟糕的情况是O(n / 2)。 Here is the code. 这是代码。

// Program to shift all even numbers in an array to left and odd to the right. Order of digits is not important.

import java.util.*;
import java.lang.*;
import java.io.*;


class Rearrange
{
public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    int[] array = new int[] {1,2,3,4,5,6,7};

    // keep two pointers.
    int odd, even;
    odd = 0; 
    even = array.length-1;
    int i;

    // Code to re-arrange the contents of the array
    i=0;
    while(i<array.length){
        if(array[i]%2!=0){
            odd = i;
            break;
        }
        i++;    
    }

    i=array.length-1;
    while(i>=0){
        if(array[i]%2==0){
            even = i;
            break;
        }
        i--;    
    }

    while(odd<even){
        if((array[odd]%2!=0) && (array[even]%2==0)){
            // swap contents
            array[odd] = array[odd] + array[even];
            array[even] = array[odd] - array[even];
            array[odd] = array[odd] - array[even];
            odd++;
            even--;
        }

        else if(array[odd]%2==0){
            odd++;
        }

        else if(array[even]%2!=0){
            even--;
        }

        else
            continue;
    }
    for(int val : array)
        System.out.println(val+" ");

}
}

For any algorithm without sufficient information about structure of data you cannot do it in less than O(N) where N is the input size because if you do it faster that means you are not considering a part of the input hence algorithm might be incorrect. 对于没有足够数据结构信息的任何算法,您都不能在小于O(N)下完成,其中N是输入大小,因为如果执行得更快,则意味着您没有考虑输入的一部分,因此算法可能不正确。

Here is in-place code for you problem :- 这是您遇到问题的就地代码:-

int i=0,j=n-1;

while(i<j) {

  if(arr[i]%2==0) {

     i++;
  }

  else {

     swap(arr[i],arr[j]);
     j--;

  }

} 

I don't see how is your code O(N/2) . 我看不到您的代码O(N/2) If array contains all odd or all even then one of the while loop will iterate through all n elements. 如果数组包含所有奇数或所有偶数,则while循环之一将遍历所有n个元素。

Instead of splitting code like you have done to get first odd/even numbers you can do something like quick sort, pivot element being last element and instead of separating less than and greater than you can check your criteria . 与其像拆分代码那样先获取第一个奇数/偶数quick sort, pivot element being last element不如执行quick sort, pivot element being last element ,而不是分离小于和大于check your criteria

    int i = 0;
    int j = arr.length -1;

    while(i < j){
        if(arr[i]%2 != 0){
            i++; 
        }
        if(arr[j]%2 == 0){
            j--;
        }
        //swap
        arr[i] = arr[i] + arr[j];
        arr[j] = arr[i] - arr[j];
        arr[i] = arr[i] - arr[j];
    }

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

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