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:
- Iterate from left to right till it increasing (suppose index to leftIndexEnd)
- Iterate from right to left till it decrease (suppose index to rightIndexStart)
- Find index minIndex between leftIndexEnd and rightIndexStart which has minimum value
- Find index maxIndex between leftIndexEnd and rightIndexStart which has maximum value
- Find leftIndex between 0 to leftIndexEnd which comes just before array[minIndex]
- Find rightIndex size – 1 to rightIndexStart which comes just before array[maxIndex]
- 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}