@Test public void shouldWorkWithAnArrayFullOfPositiveNumbersWithTwoPointersIterative() { int[] array = { 1, 2, 1, 3, 4, 6 }; splitArray.splitSwappingIterative(array); assertNegativeElementsAreBeforePositiveOnes(array); }
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--; } } } }
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--; } } } } }
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--; } } } }  }
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); }
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);  }