Sort an array where a subarray of a sorted array is in reverse order
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.
Examples:
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:
C++
#include <bits/stdc++.h>
using namespace std;
void printSorted( int a[], int n)
{
int front = -1, back = -1;
for ( int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
front = i - 1;
break ;
}
}
for ( int i = n - 2; i >= 0; i--) {
if (a[i] > a[i + 1]) {
back = i + 1;
break ;
}
}
if (front == -1 and back == -1) {
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
return ;
}
while (front <= back) {
swap(a[front], a[back]);
front++;
back--;
}
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
}
int main()
{
int a[] = { 1, 7, 6, 5, 4, 3, 2, 8 };
int n = sizeof (a) / sizeof (a[0]);
printSorted(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printSorted( int a[], int n)
{
int front = - 1 , back = - 1 ;
for ( int i = 1 ; i < n; i++)
{
if (a[i] < a[i - 1 ])
{
front = i - 1 ;
break ;
}
}
for ( int i = n - 2 ; i >= 0 ; i--)
{
if (a[i] > a[i + 1 ])
{
back = i + 1 ;
break ;
}
}
if (front == - 1 && back == - 1 )
{
for ( int i = 0 ; i < n; i++)
System.out.println(a[i] + " " );
return ;
}
while (front <= back)
{
int temp = a[front];
a[front] = a[back];
a[back] = temp;
front++;
back--;
}
for ( int i = 0 ; i < n; i++)
System.out.print(a[i] + " " );
}
public static void main (String[] args)
{
int a[] = { 1 , 7 , 6 , 5 , 4 , 3 , 2 , 8 };
int n = a.length;
printSorted(a, n);;
}
}
|
Python3
def printSorted(a, n):
front = - 1
back = - 1
for i in range ( 1 , n, 1 ):
if (a[i] < a[i - 1 ]):
front = i - 1
break
i = n - 2
while (i > = 0 ):
if (a[i] > a[i + 1 ]):
back = i + 1
break
i - = 1
if (front = = - 1 and back = = - 1 ):
for i in range ( 0 , n, 1 ):
print (a[i], end = " " )
return
while (front < = back):
temp = a[front]
a[front] = a[back]
a[back] = temp
front + = 1
back - = 1
for i in range ( 0 , n, 1 ):
print (a[i], end = " " )
if __name__ = = '__main__' :
a = [ 1 , 7 , 6 , 5 , 4 , 3 , 2 , 8 ]
n = len (a)
printSorted(a, n)
|
C#
using System;
class GFG
{
static void printSorted( int []a, int n)
{
int front = -1, back = -1;
for ( int i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
front = i - 1;
break ;
}
}
for ( int i = n - 2; i >= 0; i--)
{
if (a[i] > a[i + 1])
{
back = i + 1;
break ;
}
}
if (front == -1 && back == -1)
{
for ( int i = 0; i < n; i++)
{
Console.Write(a[i] + " " );
}
return ;
}
while (front <= back)
{
swap(a, front, back);
front++;
back--;
}
for ( int i = 0; i < n; i++)
{
Console.Write(a[i] + " " );
}
}
static void swap( int [] a, int front,
int back)
{
int c = a[front];
a[front] = a[back];
a[back] = c;
}
public static void Main()
{
int []a = {1, 7, 6, 5, 4, 3, 2, 8};
int n = a.Length;
printSorted(a, n);
}
}
|
PHP
<?php
function printSorted( $a , $n )
{
$front = -1; $back = -1;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] < $a [ $i - 1])
{
$front = $i - 1;
break ;
}
}
for ( $i = $n - 2; $i >= 0; $i --)
{
if ( $a [ $i ] > $a [ $i + 1])
{
$back = $i + 1;
break ;
}
}
if ( $front == -1 && $back == -1)
{
for ( $i = 0; $i < $n ; $i ++)
echo $a [ $i ] . " " ;
return ;
}
while ( $front <= $back )
{
$temp = $a [ $front ];
$a [ $front ] = $a [ $back ];
$a [ $back ] = $temp ;
$front ++;
$back --;
}
for ( $i = 0; $i < $n ; $i ++)
echo $a [ $i ] . " " ;
}
$a = array (1, 7, 6, 5, 4, 3, 2, 8);
$n = sizeof( $a );
printSorted( $a , $n );
|
Javascript
<script>
function printSorted(a , n) {
var front = -1, back = -1;
for (i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
front = i - 1;
break ;
}
}
for (i = n - 2; i >= 0; i--) {
if (a[i] > a[i + 1]) {
back = i + 1;
break ;
}
}
if (front == -1 && back == -1) {
for (i = 0; i < n; i++)
document.write(a[i] + " " );
return ;
}
while (front <= back) {
var temp = a[front];
a[front] = a[back];
a[back] = temp;
front++;
back--;
}
for (i = 0; i < n; i++)
document.write(a[i] + " " );
}
var a = [ 1, 7, 6, 5, 4, 3, 2, 8 ];
var n = a.length;
printSorted(a, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...