[Number Theory] C018_Array I sorted by parity (Double list Storage | Double Queue Simulation | Double Pointer)

1. Title Description

Given an array A of non-negative integers, return an array consisting of all the even elements of A, 
followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

2. Questions

There are three ways to solve problems:

  • Double list s store odd and even numbers separately
  • Dual-ended Queue Storage
  • Double pointer (sort based on interchange)

Method 1: Double list storage

All even numbers in the array AAA are stored separately in the evenList evenList, and all odd numbers are stored separately in the oddListoddList evenList. Finally, the evenList evenList evenList is traversed first, then the oddListoddList oddList.

public int[] sortArrayByParity(int[] A) {

    ArrayList<Integer> evenList = new ArrayList<>();
    ArrayList<Integer> oddList = new ArrayList<>();

    for (int i = 0; i < A.length; i++) {
        if((A[i] & 1) == 0) evenList.add(A[i]);
        else                oddList.add(A[i]);
    }

    int i = 0;
    for(int even : evenList) {  // Even number in front
        A[i++] = even;
    }
    for(int odd : oddList) {    // Odd numbers behind
        A[i++] = odd; 
    }
    return A;
}

Based on the above idea of storing even numbers in evenListevenList evenList first and odd numbers in oddListoddList oddList later, we can improve the code by using an array arrarrarr arrarrarr plus a temporary pointer jjj:

public int[] sortArrayByParity(int[] A) {

    int[] arr = new int[A.length];
    int j = 0;  // Temporary Pointer

    for (int i = 0; i < A.length; i++) {
      if ((A[i] & 1) == 0)  // Even number in front
        arr[j++] = A[i];
    }
    
    for (int i = 0; i < A.length; i++) {
      if ((A[i] & 1) == 1)  // Odd numbers behind
        arr[j++] = A[i];
    }
    return arr;
  }

Complexity analysis

  • Time complexity: O(N))O(N))O(N)) O(N), N N is the number of elements in the array AAA.
  • Spatial Complexity: O(N)O(N)O(N), the former uses two listlistlists to store data, but the total number of listlistlists is NNN; the latter uses a new array of NNN sizes to store data

Method 2: Double-ended queue

Put the first number in the listlistlist, and the remaining numbers are odd or even, and odd numbers are added to the end of the queue, and even numbers are added to the top of the queue.

  • The first number does not need to be considered odd or even because its position is not fixed.
public int[] sortArrayByParity(int[] A) {

    LinkedList<Integer> list = new LinkedList<>();
    list.addLast(A[0]);

    for (int i = 1; i < A.length; i++) {
        if((A[i] & 1) == 0)  list.addFirst(A[i]); // Even number first
        else                 list.addLast(A[i]);  // Odd Play Last
    }

    int i = 0;
    for(int n : list) {
        A[i++] = n;
    }
    return A;
}

Complexity analysis

  • Time complexity: O(N))O(N))O(N)) O(N), N N is the number of elements in the array AAA.
  • Spatial complexity: O(N)O(N)O(N), which uses a listlistlist to store data.

Method 3: Double Pointer

The first two methods are left-to-right traversal, check the parity of elements one by one, in fact, based on the idea of exchange sorting, from how?Exchange requires two elements, so traversing the array from both ends to the middle is essential.

  • Ordinary sorting compares the size of elements.
  • This compares parity.

The Title requires that the result columns be in even-odd order, and that even numbers at the beginning can be exchanged with odd numbers at the end, regardless of size.

public int[] sortArrayByParity(int[] A) {

    int Even = 0;
    int Odd = A.length - 1;

    while (Even < Odd) {
        if( (A[Even] & 1) == 1 && (A[Odd] & 1) == 0 ) {
            swap(A, Even, Odd);     // If Even points to odd and Odd points to even, then swap
        }
        else if( (A[Even] & 1) == 0)// If Even points to an even number, Even continues to explore to the right
            Even++;
        else if( (A[Odd] & 1) == 1) // If Odd points to an odd number, Odd continues to explore to the left
            Odd--;
    }
    return A;
}
  
private static void swap(int[] A, int i, int j) {
    int t = A[i];
    A[i] = A[j];
    A[j] = t;
}

Complexity analysis

  • Time complexity: O(N))O(N))O(N)) O(N), N N is the number of elements in the array AAA.
  • Spatial complexity: O(1)O(1)O(1), no additional storage space.
342 original articles were published, 54 were praised, 20,000 visits+
Private letter follow

Posted on Thu, 06 Feb 2020 22:42:46 -0500 by ts10