# Sorting array except elements in a subarray

This article is about how we can sort an array by neglecting a subarray of elements present in the same array. We will discuss two approaches for the same. The first approach is a brute force approach with time complexity O(n*n) while the second approach is by using an additional space to keep the sorted part of array other than the subarray. The time complexity of second approach is better i.e., O(nlogn).

## Problem Statement

We are given an array of positive integers “nums” and two indices of the same array namely- left and right and we have to partially sort the array.

Our task is to sort the given array “nums” in ascending order but the subarray represented between the indices left and right should remain unchanged.

In other words, the elements of the subarray should be at the same position as they were before performing any action on the array.

Example 1

Input array: nums[] = {18, 1, 12, 6, 16,10}
left = 1
right = 3
Resultant array: {10, 1, 12, 6, 18, 16}


Here, we have sorted the array nums while keeping the elements of the subarray arr[1…3] at the same position.

Example 2

Input array: nums[] = { 1, 8, 6, 2, 4}
left = 2
right = 3
Resultant array: {1, 4, 6, 2, 8}


## Approach 1: Brute Force approach

This approach divides the array into three parts, the left part, right part and subarray itself.

In this approach, we will firstly sort the left part of the given array i.e. The array from index 0 to lower bound of the given range. Inside the for loop, we will separately sort the left part and right part of the given array. After this, we will sort the right part of the subarray.

### Example

Below is an implementation in C++ of the approach discussed above −

#include <bits/stdc++.h>
using namespace std;
void sortingexceptsubarray (int nums[], int size, int left, int right){
int iterator, j;
for(iterator = 0; iterator<left; iterator++) {
for(j = iterator+1; j<left; j++) {
if(nums[iterator] > nums[j])
swap(nums[iterator], nums[j]);
}
for (j = right+1; j<size; j++) {
if(nums[iterator] > nums[j])
swap(nums[iterator], nums[j]);
}
}
for (iterator = right+1; iterator<size; iterator++) {
for (j = iterator+1; j<size; j++) {
if(nums[iterator] > nums[j])
swap(nums[iterator], nums[j]);
}
}
}
int main(){
int nums[] = { 15,5,8,6,16,11 };
int size = sizeof(nums) / sizeof(nums);
int left = 1, right = 3;
sortingexceptsubarray(nums, size, left, right );
for (int iterator = 0; iterator<size; iterator++) {
cout<<nums[iterator]<<" ";
}
}


### Output

On compilation, the above C++ program will produce the following output −

11 5 8 6 15 16


## Approach 2

Here we will be solving the given problem using additional space. We will use another array to perform this approach.

First, we will copy the elements of the array except the ones present in the subarray in a separate array. Now, we will sort the new subarray in ascending order and at last, we will copy and paste the elements into the original array to get the final partially sorted array.

### Algorithm

• STEP 1 − Create a new array copy of size N - (upperbound - lowerbound + 1).

• STEP 2 − Fill the new array “copy” with elements from the original array except the STEP of given indices.

• STEP 3 − Now, we will sort the array “copy” in ascending order.

• STEP 4 − Copy the elements from the array “copy” to our original array “nums” except the range given through the indices.

• STEP 5 − Return the new array and print it.

Let us now examine an example using this method −

Input array: nums[] = { 1, 8, 6, 2, 4}
left = 2
right = 3


### Explanation

• Created an array “copy” of size = 3;

• Copied the desired elements of the array to “copy”.

• The array “copy” will be: {1, 8, 4}.

• After sorting the array “copy”, we get: {1, 4, 8}

• Copied the sorted array “copy” to the original array “nums” except the indices: 2,3

• The finally generated array “nums” is: {1, 4, 6, 2, 8}

### Example

Below is an implementation of the above approach using C++.

#include <bits/stdc++.h>
using namespace std;
void sortExceptUandL(int num[], int lowerbound, int upperbound, int N){
int copy [N - (upperbound - lowerbound + 1)];
for (int iterator = 0; iterator < lowerbound; iterator++)
copy[iterator] = num[iterator];
for (int iterator = upperbound+1; iterator < N; iterator++)
copy[lowerbound + (iterator - (upperbound+1))] = num[iterator];
sort (copy, copy + N - (upperbound - lowerbound + 1));
for (int iterator = 0; iterator < lowerbound; iterator++)
num[iterator] = copy[iterator];
for (int iterator = upperbound+1; iterator < N; iterator++)
num[iterator] = copy [lowerbound + (iterator - (upperbound+1))];
}
int main(){
int num[] = { 5, 4, 3, 12, 14, 9 };
int N = sizeof(num) / sizeof(num);
int lowerbound = 2, upperbound = 4;
sortExceptUandL(num, lowerbound, upperbound, N);
for (int iterator = 0; iterator < N; iterator++)
cout << num[iterator] << " ";
}


### Output

On compilation, the above program will produce the following output −

4 5 3 12 14 9


The time complexity for this approach is O(n*logn ). The space complexity for this approach is O(n).

In this article, we have read about two different methods to perform the given task i.e., to sort the given array ignoring the subarray formed between the given range of lower bound and upper bound.

Updated on: 11-Apr-2023

260 Views 