[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/wodgithub/algorithm/master/PuzzleCoding/src/StablePartition.java [Back]  [Original]

import java.util.Arrays;

/**
 * Given an array of positive and negative integers,
 * re-arrange it so that positives on one end and negatives on the other,
 * but retain the original order of appearance,
 * e.g. 2,7,-5, 9,-1, 10,-3 => -5, -1, -3, 2, 7, 9, 10.
 * Do it in-place without extra space, in O(nlogn).
 */

public class StablePartition {
    public static void main(String[] args) {
        // int[] a = new int[]{1, -5, 2, 3, -4, -2, 9, 3, -1};
        int[] a = new int[]{1, -5, 7, 3, -1, -2, 9, 3, -3};
        //int[] a = new int[]{1, -3, 2,-1,3};

        stable2WayPartition(a);

        System.out.println(Arrays.toString(a));
    }

    public static void stable2WayPartition(int[] a) {
        if (a == null || a.length = right)
            return;

        int mid = (left + right) / 2;
        stable2WayPartition(a, left, mid);
        stable2WayPartition(a, mid + 1, right);

        int i = left, j = mid + 1;
        while (i 

Web Proxy Viewer  |  New URL  |  Original Page