Sort the array in a given index range
Last Updated :
13 Sep, 2022
Given an array arr[] of N integers and an index range [a, b]. The task is to sort the array in this given index range i.e., sort the elements of the array from arr[a] to arr[b] while keeping the positions of other elements intact and print the modified array.
Note: There is no relation between a and b i.e., a can be less than, equal to or greater than b. Also, 0 ? a, b < N
Examples:
Input: arr[] = {7, 8, 4, 5, 2}, a = 1, b = 4
Output: 7 2 4 5 8
For the index range [1, 4] we get the elements 8, 4, 5 and 2
On sorting these elements we get 2, 4, 5 and 8.
So the array is modified as {7, 2, 4, 5, 8}
Input: arr[] = {20, 10, 3, 8}, a = 3, b = 1
Output: 20 3 8 10
Approach:
- Make a temporary array of the elements for the given index range of the array.
- Sort this temporary array.
- Now modify the original array with these sorted elements of temporary array for the given index range.
Below is the implementation of the above approach:
C++
// C++ program to sort the
// array in a given index range
#include <bits/stdc++.h>
using namespace std;
// Function to sort the elements of the array
// from index a to index b
void partSort(int arr[], int N, int a, int b)
{
// Variables to store start and
// end of the index range
int l = min(a, b);
int r = max(a, b);
// Temporary array
int temp[r - l + 1];
int j = 0;
for (int i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
// Sort the temporary array
sort(temp, temp + r - l + 1);
// Modifying original array with
// temporary array elements
j = 0;
for (int i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
// Print the modified array
for (int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
// Driver code
int main()
{
int arr[] = { 7, 8, 4, 5, 2 } ;
int a = 1 ;
int b = 4;
// length of the array
int N = sizeof(arr) / sizeof(arr[0]);
partSort(arr, N, a, b);
return 0;
}
// This code is contributed by Ryuga
Java
// Java program to sort the array in a given index range
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG {
// Function to sort the elements of the array
// from index a to index b
static void partSort(int[] arr, int N, int a, int b)
{
// Variables to store start and end of the index range
int l = Math.min(a, b);
int r = Math.max(a, b);
// Temporary array
int[] temp = new int[r - l + 1];
int j = 0;
for (int i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
// Sort the temporary array
Arrays.sort(temp);
// Modifying original array with temporary array elements
j = 0;
for (int i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
// Print the modified array
for (int i = 0; i < N; i++) {
System.out.print(arr[i] + " ");
}
}
// Driver code
public static void main(String args[])
{
int[] arr = { 7, 8, 4, 5, 2 };
int a = 1, b = 4;
// length of the array
int N = arr.length;
partSort(arr, N, a, b);
}
}
Python3
# Python 3 program to sort the
# array in a given index range
# Function to sort the elements of
# the array from index a to index b
def partSort(arr, N, a, b):
# Variables to store start and
# end of the index range
l = min(a, b)
r = max(a, b)
# Temporary array
temp = [0 for i in range(r - l + 1)]
j = 0
for i in range(l, r + 1, 1):
temp[j] = arr[i]
j += 1
# Sort the temporary array
temp.sort(reverse = False)
# Modifying original array with
# temporary array elements
j = 0
for i in range(l, r + 1, 1):
arr[i] = temp[j]
j += 1
# Print the modified array
for i in range(0, N, 1):
print(arr[i], end = " ")
# Driver code
if __name__ == '__main__':
arr = [7, 8, 4, 5, 2]
a = 1
b = 4
# length of the array
N = len(arr)
partSort(arr, N, a, b)
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to sort the array in a given index range
using System;
class GFG {
// Function to sort the elements of the array
// from index a to index b
static void partSort(int[] arr, int N, int a, int b)
{
// Variables to store start and end of the index range
int l = Math.Min(a, b);
int r = Math.Max(a, b);
// Temporary array
int[] temp = new int[r - l + 1];
int j = 0;
for (int i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
// Sort the temporary array
Array.Sort(temp);
// Modifying original array with temporary array elements
j = 0;
for (int i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
// Print the modified array
for (int i = 0; i < N; i++) {
Console.Write(arr[i] + " ");
}
}
// Driver code
public static void Main()
{
int[] arr = { 7, 8, 4, 5, 2 };
int a = 1, b = 4;
// length of the array
int N = arr.Length;
partSort(arr, N, a, b);
}
}
// This code is contributed by anuj_67
PHP
<?php
# PHP program to sort the
# array in a given index range
// Function to sort the elements of the array
// from index a to index b
function partSort( $arr, $N, $a, $b)
{
// Variables to store start and
// end of the index range
$l = min($a, $b);
$r = max($a, $b);
// Temporary array
$temp = array();
$j = 0;
for ($i = $l; $i <= $r; $i++) {
$temp[$j] = $arr[$i];
$j++;
}
// Sort the temporary array
sort($temp);
// Modifying original array with
// temporary array elements
$j = 0;
for ($i = $l; $i <= $r; $i++) {
$arr[$i] = $temp[$j];
$j++;
}
// Print the modified array
for ($i = 0; $i < $N; $i++) {
echo $arr[$i]." " ;
}
}
$arr = array( 7, 8, 4, 5, 2 ) ;
$a = 1 ;
$b = 4;
// length of the array
$N = count($arr);
partSort($arr, $N, $a, $b);
//This code is contributed by 29AjayKumar
?>
JavaScript
<script>
// Javascript program to sort the array in a given index range
// Function to sort the elements of the array
// from index a to index b
function partSort(arr, N, a, b)
{
// Variables to store start and end of the index range
let l = Math.min(a, b);
let r = Math.max(a, b);
// Temporary array
let temp = new Array(r - l + 1);
temp.fill(0);
let j = 0;
for (let i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
// Sort the temporary array
temp.sort(function(a, b){return a - b});
// Modifying original array with temporary array elements
j = 0;
for (let i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
// Print the modified array
for (let i = 0; i < N; i++) {
document.write(arr[i] + " ");
}
}
let arr = [ 7, 8, 4, 5, 2 ];
let a = 1, b = 4;
// length of the array
let N = arr.length;
partSort(arr, N, a, b);
</script>
Complexity Analysis:
- Time Complexity: O(nlog(n))
- Auxiliary Space: O(n)
Below is a direct solution using Arrays.sort()
C++
// C++ program to sort the array in a given index range
#include<bits/stdc++.h>
using namespace std;
// Function to sort the elements of the array
// from index a to index b
void partSort(int arr[], int N, int a, int b)
{
// Variables to store start and end
// of the index range
int l = min(a, b);
int r = max(a, b);
vector<int> v(arr, arr + N);
// Sort the subarray from arr[l] to
// arr[r]
sort(v.begin() + l, v.begin() + r + 1);
// Print the modified array
for (int i = 0; i < N; i++)
cout << v[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 7, 8, 4, 5, 2 };
int a = 1, b = 4;
int N = sizeof(arr)/sizeof(arr[0]);
partSort(arr, N, a, b);
}
// This code is contributed by
// Sanjit_Prasad
Java
// Java program to sort the array in a given index range
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG {
// Function to sort the elements of the array
// from index a to index b
static void partSort(int[] arr, int N, int a, int b)
{
// Variables to store start and end
// of the index range
int l = Math.min(a, b);
int r = Math.max(a, b);
// Sort the subarray from arr[l] to
// arr[r]
Arrays.sort(arr, l, r + 1);
// Print the modified array
for (int i = 0; i < N; i++)
System.out.print(arr[i] + " ");
}
// Driver code
public static void main(String args[])
{
int[] arr = { 7, 8, 4, 5, 2 };
int a = 1, b = 4;
int N = arr.length;
partSort(arr, N, a, b);
}
}
Python3
# Python3 program to sort the
# array in a given index range
# Function to sort the elements of
# the array from index a to index b
def partSort(arr, N, a, b):
# Variables to store start and
# end of the index range
l = min(a, b)
r = max(a, b)
arr = (arr[0 : l] +
sorted(arr[l : r + 1]) +
arr[r : N])
# Print the modified array
for i in range(0, N, 1):
print(arr[i], end = " ")
# Driver code
if __name__ == '__main__':
arr = [ 7, 8, 4, 5, 2 ]
a = 1
b = 4
# Length of the array
N = len(arr)
partSort(arr, N, a, b)
# This code is contributed by grand_master
C#
// C# program to sort the array in a given index range
using System;
class GFG {
// Function to sort the elements of the array
// from index a to index b
static void partSort(int[] arr, int N, int a, int b)
{
// Variables to store start and end
// of the index range
int l = Math.Min(a, b);
int r = Math.Max(a, b);
// Sort the subarray from arr[l] to
// arr[r]
Array.Sort(arr, l, r);
// Print the modified array
for (int i = 0; i < N; i++)
Console.Write(arr[i] + " ");
}
// Driver code
static void Main()
{
int[] arr = { 7, 8, 4, 5, 2 };
int a = 1, b = 4;
int N = arr.Length;
partSort(arr, N, a, b);
}
}
// This code is contributed by mits
JavaScript
<script>
// javascript program to sort the array in a given index range
// Function to sort the elements of the array
// from index a to index b
function swap(arr, xp, yp)
{
var temp = arr[xp];
arr[xp] = arr[yp];
arr[yp] = temp;
}
function partSort(arr , N , a , b)
{
// Variables to store start and end
// of the index range
var l = Math.min(a, b);
var r = Math.max(a, b);
// Sort the subarray from arr[l] to
// arr[r]
//.sort(arr, l, r + 1);
var i, j;
for (i = l; i < r + 1 + 1; i++)
{
for (j = l; j < r - i + 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr, j, j + 1);
}
}
}
// Print the modified array
for (i = 0; i < N; i++)
document.write(arr[i] + " ");
}
// Driver code
var arr = [ 7, 8, 4, 5, 2 ];
var a = 1, b = 4;
var N = arr.length;
partSort(arr, N, a, b);
// This code is contributed by gauravrajput1
</script>
Complexity Analysis:
- Time Complexity: O(nlog(n))
- Auxiliary Space: O(n)
Similar Reads
Java Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave-like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
4 min read
Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort()
2 min read
Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO
2 min read
Java Program for 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 =
3 min read
Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically.
7 min read
Sort 2D array lexicographically Given a 2D array arr[] having N rows of variable size, the task is to sort the array in lexicographical order i.e., sort each row lexicographically and then sort those sorted rows. Examples: Input: arr[][] = { {23}, {59}, {23, 59} }Output: { {23}, {23, 59}, {59} }Explanation: The rows are sorted lex
6 min read
Java Program For Converting Array Into Zig-Zag Fashion Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a c e . Example: Input: arr[] = {4, 3, 7, 8, 6, 2, 1} Output: arr[] = {3, 7, 4, 8, 2, 6, 1} Input: arr[] = {1, 4, 3, 2} Output: arr[] = {1, 4, 2, 3} Recommende
3 min read
Java Program for Odd-Even Sort / Brick Sort This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in
2 min read
Java Program for ShellSort In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h'th element is sorted. Java // Java implementation of ShellSort class ShellSort { /* An utility function to print array of si
2 min read
C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C
3 min read