SplitArray { public void splitSwappingIterative(int[] array) { if (array == null) { throw new IllegalArgumentException("Array passed as parameter can't be null."); } int left = 0; int right = array.length - 1; while (left < right) { boolean shouldChangeLeft = array[left] >= 0; boolean shouldChangeRight = array[right] < 0; if (shouldChangeLeft && shouldChangeRight) { swap(array, left, right); left++; right--; } else { if (!shouldChangeLeft) { left++; } else if (!shouldChangeRight) { right--; } } } }  void splitSorting(int[] array); void splitSwappingIterative(int[] array); void splitSwappingRecursive(int[] array);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullArraysWithTwoPointersIterative() { splitArray.splitSwappingIterative(null); }
@Test public void shouldSupportEmptyArraysWithTwoPointersIterative() { int[] array = new int[0]; splitArray.splitSwappingIterative(array); assertNegativeElementsAreBeforePositiveOnes(array); }
@Test public void shouldWorkWithAnArrayFullOfPositiveNumbersWithTwoPointersIterative() { int[] array = { 1, 2, 1, 3, 4, 6 }; splitArray.splitSwappingIterative(array); assertNegativeElementsAreBeforePositiveOnes(array); }
@Test public void shouldWorkWithAnArrayFullOfNegativeNumbersWithTwoPointersIterative() { int[] array = { 1, 2, 1, 3, 4, 6 }; splitArray.splitSwappingIterative(array); assertNegativeElementsAreBeforePositiveOnes(array); }
@Test public void shouldMoveNegativeElementsToTheLeftWithTwoPointersIterative() { int[] array = { 1, 2, -1, -3, 4, -6 }; splitArray.splitSwappingIterative(array); assertNegativeElementsAreBeforePositiveOnes(array); }
@Test public void shouldMoveNegativeElementsToTheLeftWithWithMoreNegativeElementsTwoPointersIterative() { int[] array = { 1, -2, -1, -3, 4, -6 }; splitArray.splitSwappingIterative(array); assertNegativeElementsAreBeforePositiveOnes(array); }