Move all odd positioned element to left half and even positioned to right half in-place

前端 未结 5 1658
别跟我提以往
别跟我提以往 2020-12-01 03:49

Given an array with positive and negative integers, move all the odd indexed elements to the left and even indexed elements to the right.

The difficult part of the p

5条回答
  •  清歌不尽
    2020-12-01 04:42

    Here's a Java implementation of Peiyush Jain's algorithm:

    import java.util.Arrays;
    
    public class InShuffle {
        public static void main(String[] args) {
            Integer[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    
            inShuffle(nums);
    
            System.out.println(Arrays.toString(nums));
        }
    
        public static  void inShuffle(T[] array) {
            if (array == null) {
                return;
            }
    
            inShuffle(array, 0, array.length - 1);
        }
    
        private static  void inShuffle(T[] array, int startIndex, int endIndex) {
            while (endIndex - startIndex + 1 > 1) {
                int size = endIndex - startIndex + 1;
                int n = size / 2;
                int k = (int)Math.floor(Math.log(size + 1) / Math.log(3));
                int m = (int)(Math.pow(3, k) - 1) / 2;
    
                rotateRight(array, startIndex + m, startIndex + n + m - 1, m);
    
                for (int i = 0, cycleStartIndex = 0; i < k; ++i, cycleStartIndex = cycleStartIndex * 3 + 2) {
                    permuteCycle(array, startIndex, cycleStartIndex, 2 * m + 1);
                }
    
                endIndex = startIndex + 2 * n - 1;
                startIndex = startIndex + 2 * m;
            }
        }
    
        private static  void rotateRight(T[] array, int startIndex, int endIndex, int amount) {
            reverse(array, startIndex, endIndex - amount);
            reverse(array, endIndex - amount + 1, endIndex);
            reverse(array, startIndex, endIndex);
        }
    
        private static  void reverse(T[] array, int startIndex, int endIndex) {
           for (int leftIndex = startIndex, rightIndex = endIndex; leftIndex < rightIndex; ++leftIndex, --rightIndex) {
               swap(array, leftIndex, rightIndex);
           }
        }
    
        private static  void swap(T[] array, int index1, int index2) {
            T temp = array[index1];
            array[index1] = array[index2];
            array[index2] = temp;
        }
    
        private static  void permuteCycle(T[] array, int offset, int startIndex, int mod) {
            for (int i = ((2 * startIndex + 2) % mod) - 1; i != startIndex; i = ((2 * i + 2) % mod) - 1) {
                swap(array, offset + i, offset + startIndex);
            }
        }
    }
    

    And doing an out-shuffle is as simple as:

    public static  void outShuffle(T[] array) {
        if (array == null) {
           return;
        }
    
        inShuffle(array, 1, array.length - 1);
    }
    

提交回复
热议问题