Sort sub array

Sort sub array

Given an array of integers, find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n-m. i.e. find smallest sequence.
Algorithm:
  1. Iterate from left to right till it increasing (suppose index to leftIndexEnd)
  2. Iterate from right to left till it decrease (suppose index to rightIndexStart)
  3. Find index minIndex between leftIndexEnd and rightIndexStart which has minimum value
  4. Find index maxIndex between leftIndexEnd and rightIndexStart which has maximum value
  5. Find leftIndex between 0 to leftIndexEnd which comes just before array[minIndex]
  6. Find rightIndex size – 1 to rightIndexStart which comes just before array[maxIndex]
  7. Return leftIndex and rightIndex

Latest Source Code:
Github: SortSubArray.java


Output:

Array: [1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19]
Result: Result{m=3, n=9}
Author: Hrishikesh Mishra