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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read