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++
// CPP program to sort all elements except
// given subarray.
#include <bits/stdc++.h>
using namespace std;
// Sort whole array a[] except elements in
// range a[l..r]
void sortExceptUandL(int a[], int l, int u, int n)
{
// Copy all those element that need
// to be sorted to an auxiliary
// array b[]
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 the array b
sort(b, b + n - (u - l + 1));
// Copy sorted elements back to a[]
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))];
}
// Driver code
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
// Java program to sort all elements except
// given subarray.
import java.util.Arrays;
import java.io.*;
public class GFG {
// Sort whole array a[] except elements in
// range a[l..r]
public static void sortExceptUandL(int a[],
int l, int u, int n)
{
// Copy all those element that need
// to be sorted to an auxiliary
// array b[]
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];
// sort the array b
Arrays.sort(b);
// Copy sorted elements back to a[]
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))];
}
// Driver code
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] + " ");
}
}
// This code is contributed by Manish Shaw
// (manishshaw1)
Python3
# Python3 program to sort all elements
# except given subarray.
# Sort whole array a[] except elements in
# range a[l..r]
def sortExceptUandL(a, l, u, n) :
# Copy all those element that need
# to be sorted to an auxiliary
# array b[]
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]
# sort the array b
b.sort()
# Copy sorted elements back to a[]
for i in range(0, l) :
a[i] = b[i]
for i in range(u+1, n) :
a[i] = b[l + (i - (u+1))]
# Driver code
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="")
# This code is contributed by
# Manish Shaw (manishshaw1)
C#
// C# program to sort all elements except
// given subarray.
using System;
using System.Collections.Generic;
class GFG {
// Sort whole array a[] except elements in
// range a[l..r]
static void sortExceptUandL(int []a, int l,
int u, int n)
{
// Copy all those element that need
// to be sorted to an auxiliary
// array b[]
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];
// sort the array b
Array.Sort<int>(b, 0, n - (u - l + 1));
// Copy sorted elements back to a[]
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))];
}
// Driver code
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] + " ");
}
}
// This code is contributed by Manish Shaw
// (manishshaw1)
PHP
<?php
// PHP program to sort all
// elements except given subarray.
// Sort whole array a[] except
// elements in range a[l..r]
function sortExceptUandL($a, $l,
$u, $n)
{
// Copy all those element
// that need to be sorted
// to an auxiliary array b[]
$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 the array b
sort($b);
// Copy sorted elements
// back to a[]
for ($i = 0; $i < $l; $i++)
$a[$i] = $b[$i];
for ($i = $u + 1; $i < $n; $i++)
$a[$i] = $b[$l + ($i - ($u + 1))];
}
// Driver code
$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]. " ");
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
JavaScript
<script>
// JavaScript program to sort all elements except
// given subarray.
// Sort whole array a[] except elements in
// range a[l..r]
function sortExceptUandL(a, l, u, n)
{
// Copy all those element that need
// to be sorted to an auxiliary
// array b[]
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];
// sort the array b
b.sort();
// Copy sorted elements back to a[]
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))];
}
// Driver code
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] + " ");
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Similar Reads
Sorting all array elements except one Given an array, a positive integer, sort the array in ascending order such that the element at index K in the unsorted array stays unmoved and all other elements are sorted. Examples: Input : arr[] = {10, 4, 11, 7, 6, 20} k = 2; Output : arr[] = {4, 6, 11, 7, 10, 20} Input : arr[] = {30, 20, 10} k =
6 min read
Add elements in start to sort the array | Variation of Stalin Sort Stalin sort (also 'dictator sort' and 'trump sort') is a nonsensical 'sorting' algorithm in which each element that is not in the correct order is simply eliminated from the list. This sorting algorithm is a less destructive variation of Stalin sort, that will actually sort the list: In this case, t
6 min read
Sort an array by left shifting digits of array elements Given an array arr[] consisting of N positive integers, the task is to left-shift the digits of array elements such that the array modifies to a sorted form. If multiple solutions exist, then print any one of them. Otherwise, print -1. Examples: Input: arr[] = { 511, 321, 323, 432 } Output: { 115, 1
8 min read
Count subarrays for every array element in which they are the minimum Given an array arr[] consisting of N integers, the task is to create an array brr[] of size N where brr[i] represents the count of subarrays in which arr[i] is the smallest element. Examples: Input: arr[] = {3, 2, 4} Output: {1, 3, 1} Explanation: For arr[0], there is only one subarray in which 3 is
15+ min read
Remove elements to make array sorted Given an array of integers, the task is to remove elements from the array to make the array sorted. That is, remove the elements which do not follow an increasing order. Examples: Input: arr[] = {1, 2, 4, 3, 5, 7, 8, 6, 9, 10} Output: 1 2 4 5 7 8 9 10 Input: arr[] = {10, 12, 9, 5, 2, 13, 14} Output:
8 min read
Sort Array by splitting into subarrays where each element belongs to only subarray Given an array arr[] of N distinct integers, the task is to check if it is possible to sort the array in increasing order by performing the following operations in order exactly once: Split the array arr[] into exactly Y(1 <= Y <= N) non-empty subarrays such that each element belongs to exactl
7 min read
Find missing elements of a range Given an array, arr[0..n-1] of distinct elements and a range [low, high], find all numbers that are in a range, but not the array. The missing elements should be printed in sorted order.Examples: Input: arr[] = {10, 12, 11, 15}, low = 10, high = 15Output: 13, 14Input: arr[] = {1, 14, 11, 51, 15}, lo
15+ min read
Sorting an array in Bash using Bubble sort Prerequisite: Bubble Sort Given an array arr sort the array in ascending order using bash scripting. Examples: Input : 9 7 2 5 Output : Array in sorted order : 2 5 7 9 Approach : For sorting the array bubble sort is the simplest technique. Bubble sort works by swapping the adjacent elements if they
2 min read
Sort given Array using at most N cyclic shift on any subarray Given an array arr[] containing N integers, with duplicates. The task is to sort the array in increasing order using at most N cyclic shift on any sub-array. Cyclic shift on any sub-array means cut out any subarray from the given array, use cyclic shift (rotate) in it by any offset, and put it back
8 min read