Given an array of N numbers where a subarray is sorted in descending order and rest of the numbers in the array are in ascending order. The task is to sort an array where a subarray of a sorted array is in reversed order.
Input: 2 5 65 55 50 70 90
Output: 2 5 50 55 65 70 90
The subarray from 2nd index to 4th index is in reverse order.
So the subarray is reversed, and the sorted array is printed.
Input: 1 7 6 5 4 3 2 8
Output: 1 2 3 4 5 6 7 8
A naive approach will be to sort the array and print the array. Time Complexity of this approach will be O(N log n).
An efficient approach will be to find and store the starting index and ending index of the reversed subarray. Since the subarray is in descending order and the rest of the elements are in ascending order, only reversing the subarray will sort the complete array. Reverse the subarray using two pointer approach.
Below is the implementation of the above approach:
# Python 3 program to sort an array where
# a subarray of a sorted array is in
# reversed order
# Function to print the sorted array
# by reversing the subarray
def printSorted(a, n):
front = -1
back = -1
# find the starting index of the
# reversed subarry
for i in range(1, n, 1):
if (a[i] < a[i - 1]): front = i - 1 break # find the ending index of the # reversed subarray i = n - 2 while(i >= 0):
if (a[i] > a[i + 1]):
back = i + 1
i -= 1
# if no reversed subarray is present
if (front == -1 and back == -1):
for i in range(0, n, 1):
print(a[i], end = ” “)
# swap the reversed subarray
while (front <= back): # swaps the front and back element # using c++ STL temp = a[front] a[front] = a[back] a[back] = temp # move the pointers one step # ahead and one step back front += 1 back -= 1 for i in range(0, n, 1): print(a[i], end = " ") # Driver Code if __name__ == '__main__': a = [1, 7, 6, 5, 4, 3, 2, 8] n = len(a) printSorted(a, n) # This code is contributed by # Sahil_Shelangia [tabby title="C#"]
1 2 3 4 5 6 7 8
Time Complexity: O(n)
- Sort an array according to the order defined by another array
- Program to copy the contents of one array into another in the reverse order
- Sort a nearly sorted array using STL
- Sort a nearly sorted (or K sorted) array
- Sort an array when two halves are sorted
- Sort prime numbers of an array in descending order
- Sort an array of string of dates in ascending order
- Sort only non-prime numbers of an array in increasing order
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Sort the linked list in the order of elements appearing in the array
- Merge two sorted linked lists such that merged list is in reverse order
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Maximum subarray sum in array formed by repeating the given array k times
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- First string from the given array whose reverse is also present in the same array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.