Sorting array except elements in a subarray
Last Updated :
26 May, 2022
Given an array A positive integers, sort the array in ascending order such that element in given subarray (start and end indexes are input) in unsorted array stay unmoved and all other elements are sorted.
Examples :
Input : arr[] = {10, 4, 11, 7, 6, 20}
l = 1, u = 3
Output : arr[] = {6, 4, 11, 7, 10, 20}
We sort elements except arr[1..3] which
is {11, 7, 6}.
Input : arr[] = {5, 4, 3, 12, 14, 9};
l = 1, u = 2;
Output : arr[] = {5, 4, 3, 9, 12, 14 }
We sort elements except arr[1..2] which
is {4, 3}.
Approach : Copy all elements except the given limit of given array to another array. Then sort the other array using a sorting algorithm. Finally again copy the sorted array to original array. While copying, skip given subarray.
C++
#include <bits/stdc++.h>
using namespace std;
void sortExceptUandL( int a[], int l, int u, int n)
{
int b[n - (u - l + 1)];
for ( int i = 0; i < l; i++)
b[i] = a[i];
for ( int i = u+1; i < n; i++)
b[l + (i - (u+1))] = a[i];
sort(b, b + n - (u - l + 1));
for ( int i = 0; i < l; i++)
a[i] = b[i];
for ( int i = u+1; i < n; i++)
a[i] = b[l + (i - (u+1))];
}
int main()
{
int a[] = { 5, 4, 3, 12, 14, 9 };
int n = sizeof (a) / sizeof (a[0]);
int l = 2, u = 4;
sortExceptUandL(a, l, u, n);
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
}
|
Java
import java.util.Arrays;
import java.io.*;
public class GFG {
public static void sortExceptUandL( int a[],
int l, int u, int n)
{
int b[] = new int [n - (u - l + 1 )];
for ( int i = 0 ; i < l; i++)
b[i] = a[i];
for ( int i = u+ 1 ; i < n; i++)
b[l + (i - (u+ 1 ))] = a[i];
Arrays.sort(b);
for ( int i = 0 ; i < l; i++)
a[i] = b[i];
for ( int i = u+ 1 ; i < n; i++)
a[i] = b[l + (i - (u+ 1 ))];
}
public static void main(String args[])
{
int a[] = { 5 , 4 , 3 , 12 , 14 , 9 };
int n = a.length;
int l = 2 , u = 4 ;
sortExceptUandL(a, l, u, n);
for ( int i = 0 ; i < n; i++)
System.out.print(a[i] + " " );
}
}
|
Python3
def sortExceptUandL(a, l, u, n) :
b = [ 0 ] * (n - (u - l + 1 ))
for i in range ( 0 , l) :
b[i] = a[i]
for i in range (u + 1 , n) :
b[l + (i - (u + 1 ))] = a[i]
b.sort()
for i in range ( 0 , l) :
a[i] = b[i]
for i in range (u + 1 , n) :
a[i] = b[l + (i - (u + 1 ))]
a = [ 5 , 4 , 3 , 12 , 14 , 9 ]
n = len (a)
l = 2
u = 4
sortExceptUandL(a, l, u, n)
for i in range ( 0 , n) :
print ( "{} " . format (a[i]), end = "")
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void sortExceptUandL( int []a, int l,
int u, int n)
{
int [] b = new int [n - (u-l+1)];
for ( int i = 0; i < l; i++)
b[i] = a[i];
for ( int i = u+1; i < n; i++)
b[l + (i - (u+1))] = a[i];
Array.Sort< int >(b, 0, n - (u - l + 1));
for ( int i = 0; i < l; i++)
a[i] = b[i];
for ( int i = u+1; i < n; i++)
a[i] = b[l + (i - (u+1))];
}
public static void Main()
{
int []a = { 5, 4, 3, 12, 14, 9 };
int n = a.Length;
int l = 2, u = 4;
sortExceptUandL(a, l, u, n);
for ( int i = 0; i < n; i++)
Console.Write(a[i] + " " );
}
}
|
PHP
<?php
function sortExceptUandL( $a , $l ,
$u , $n )
{
$b = array ();
for ( $i = 0; $i < $l ; $i ++)
$b [ $i ] = $a [ $i ];
for ( $i = $u + 1; $i < $n ; $i ++)
$b [ $l + ( $i - ( $u + 1))] = $a [ $i ];
sort( $b );
for ( $i = 0; $i < $l ; $i ++)
$a [ $i ] = $b [ $i ];
for ( $i = $u + 1; $i < $n ; $i ++)
$a [ $i ] = $b [ $l + ( $i - ( $u + 1))];
}
$a = array (4, 5, 3, 12, 14, 9);
$n = count ( $a );
$l = 2;
$u = 4;
sortExceptUandL( $a , $l , $u , $n );
for ( $i = 0; $i < $n ; $i ++)
echo ( $a [ $i ]. " " );
?>
|
Javascript
<script>
function sortExceptUandL(a, l, u, n)
{
let b = [];
for (let i = 0; i < l; i++)
b[i] = a[i];
for (let i = u+1; i < n; i++)
b[l + (i - (u + 1))] = a[i];
b.sort();
for (let i = 0; i < l; i++)
a[i] = b[i];
for (let i = u + 1; i < n; i++)
a[i] = b[l + (i - (u + 1))];
}
let a = [ 5, 4, 3, 12, 14, 9 ];
let n = a.length;
let l = 2, u = 4;
sortExceptUandL(a, l, u, n);
for (let i = 0; i < n; i++)
document.write(a[i] + " " );
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...