filmov
tv
Minimum length subarray of an unsorted array sorting which results in complete sorted array

Показать описание
Given an unsorted array of integers, find the shortest subarray, which upon sorting will result in complete sorted array.
Algorithm:
1: From the beginning of the array, move to element in the array up to which the elements are sorted i.e. where array[i] is greater than array[i+1]. Set startIndex = i.
2: From the end of the array, move to the element up to which the elements are sorted in reverse order i.e. where array[j-1] is greater than array[j]. Set endIndex = j.
3: Find the minimum and maximum elements in the subarray from startIndex to endIndex.
4: Search the sorted array from 0 to startIndex to find the index at which minimum element will be in sorted array say, minIndex.
5: Search the sorted array from endIndex to end of array to find the index at which maximum element will be in sorted array say, maxIndex.
6: Resultant sub array is the subarray between minIndex to maxIndex.
Order of the Algorithm:
Time Complexity: O(n)
Space Complexity: O(1)
Code and Algorithm Visualization:
Algorithm:
1: From the beginning of the array, move to element in the array up to which the elements are sorted i.e. where array[i] is greater than array[i+1]. Set startIndex = i.
2: From the end of the array, move to the element up to which the elements are sorted in reverse order i.e. where array[j-1] is greater than array[j]. Set endIndex = j.
3: Find the minimum and maximum elements in the subarray from startIndex to endIndex.
4: Search the sorted array from 0 to startIndex to find the index at which minimum element will be in sorted array say, minIndex.
5: Search the sorted array from endIndex to end of array to find the index at which maximum element will be in sorted array say, maxIndex.
6: Resultant sub array is the subarray between minIndex to maxIndex.
Order of the Algorithm:
Time Complexity: O(n)
Space Complexity: O(1)
Code and Algorithm Visualization:
-
IDeserve
-
Minimum length subarray of an unsorted array sorting which results in complete sorted array
-
minimum length unsorted subarray sorting which makes the complete array sorted
-
Given an unsorted array sort the shortest sub-array which results in sorting of the whole array
-
find indices m and n such that if you sorted elements m through n the entire array would be sorted
-
Min subarray to sort to make an unsorted array sorted
Комментарии