Sort M elements of given circular array starting from index K
Given a circular array arr[] of size N and two integers K and M, the task is to sort M array elements starting from the index K.
Examples:
Input: arr[] = {4, 1, 6, 5, 3}, K = 2, M = 3
Output: 4 1 3 5 6
Explanation: After sorting 3 array elements starting from index 2 modifies arr[] to {4, 1, 3, 5, 6}.
Input: arr[] = {67, 2, 9, 7, 1}, K = 4, M = 3
Output: 2 67 9 7 1
Explanation: After sorting 3 array elements starting from index 4 modifies arr[] to {2, 67, 9, 7, 1}.
Naive Approach: The idea is to swap the adjacent elements in the circular array if the elements of them are not in the correct order. Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printCircularArray( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
}
void sortCircularArray( int arr[], int n,
int k, int m)
{
for ( int i = 0; i < m; i++) {
for ( int j = k; j < k + m - 1; j++) {
if (arr[j % n]
> arr[(j + 1) % n]) {
swap(arr[j % n], arr[(j + 1) % n]);
}
}
}
printCircularArray(arr, n);
}
int main()
{
int arr[] = { 4, 1, 6, 5, 3 };
int K = 2, M = 3;
int N = sizeof (arr) / sizeof (arr[0]);
sortCircularArray(arr, N, K, M);
return 0;
}
|
Java
class GFG{
static void printCircularArray( int arr[], int n)
{
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
}
static void sortCircularArray( int arr[], int n,
int k, int m)
{
for ( int i = 0 ; i < m; i++) {
for ( int j = k; j < k + m - 1 ; j++) {
if (arr[j % n]
> arr[(j + 1 ) % n]) {
int t = arr[j % n];
arr[j % n] = arr[(j + 1 ) % n];
arr[(j + 1 ) % n] = t;
}
}
}
printCircularArray(arr, n);
}
public static void main (String[] args)
{
int [] arr = { 4 , 1 , 6 , 5 , 3 };
int K = 2 , M = 3 ;
int N = arr.length;
sortCircularArray(arr, N, K, M);
}
}
|
Python3
def printCircularArray(arr, n):
for i in range (n):
print (arr[i], end = " " )
def sortCircularArray(arr, n, k, m):
for i in range (m):
for j in range (k, k + m - 1 ):
if (arr[j % n] > arr[(j + 1 ) % n]):
arr[j % n], arr[(j + 1 ) % n] = (arr[(j + 1 ) % n],
arr[j % n])
printCircularArray(arr, n)
if __name__ = = "__main__" :
arr = [ 4 , 1 , 6 , 5 , 3 ]
K = 2
M = 3
N = len (arr)
sortCircularArray(arr, N, K, M)
|
C#
using System;
class GFG
{
static void printCircularArray( int []arr, int n)
{
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i] + " " );
}
}
static void sortCircularArray( int []arr, int n,
int k, int m)
{
for ( int i = 0; i < m; i++)
{
for ( int j = k; j < k + m - 1; j++)
{
if (arr[j % n]
> arr[(j + 1) % n]) {
int t = arr[j % n];
arr[j % n] = arr[(j + 1) % n];
arr[(j + 1) % n] = t;
}
}
}
printCircularArray(arr, n);
}
public static void Main ( string [] args)
{
int [] arr = { 4, 1, 6, 5, 3 };
int K = 2, M = 3;
int N = arr.Length;
sortCircularArray(arr, N, K, M);
}
}
|
Javascript
<script>
function printCircularArray(arr, n)
{
for (let i = 0; i < n; i++) {
document.write(arr[i] + " " );
}
}
function sortCircularArray(arr, n, k, m)
{
for (let i = 0; i < m; i++) {
for (let j = k; j < k + m - 1; j++) {
if (arr[j % n]
> arr[(j + 1) % n]) {
let t = arr[j % n];
arr[j % n] = arr[(j + 1) % n];
arr[(j + 1) % n] = t;
}
}
}
printCircularArray(arr, n);
}
let arr = [ 4, 1, 6, 5, 3 ];
let K = 2, M = 3;
let N = arr.length;
sortCircularArray(arr, N, K, M);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
Step-by-step approach:
- Create a new array of size M and copy the M elements starting from index K of the circular array arr[] to the new array.
- Sort the new array.
- Traverse the circular array arr[] from index K to index (K + M – 1) mod N and replace each element with the corresponding element in the sorted new array.
- If K + M is greater than N, traverse the remaining elements of the new array and replace the corresponding elements in the circular array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortCircularArray( int arr[], int n, int k, int m) {
int * tempArr = new int [m];
for ( int i = 0; i < m; i++) {
tempArr[i] = arr[(k + i) % n];
}
sort(tempArr, tempArr + m);
for ( int i = 0; i < m; i++) {
arr[(k + i) % n] = tempArr[i];
}
if (k + m > n) {
for ( int i = m; i < n - k; i++) {
arr[(k + i) % n] = tempArr[i - m];
}
}
}
int main() {
int arr[] = {4, 1, 6, 5, 3};
int K = 2, M = 3;
int N = sizeof (arr) / sizeof (arr[0]);
sortCircularArray(arr, N, K, M);
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static void sortCircularArray( int [] arr, int n,
int k, int m)
{
int [] tempArr = new int [m];
for ( int i = 0 ; i < m; i++) {
tempArr[i] = arr[(k + i) % n];
}
Arrays.sort(tempArr);
for ( int i = 0 ; i < m; i++) {
arr[(k + i) % n] = tempArr[i];
}
if (k + m > n) {
for ( int i = m; i < n - k; i++) {
arr[(k + i) % n] = tempArr[i - m];
}
}
}
public static void main(String[] args)
{
int [] arr = { 4 , 1 , 6 , 5 , 3 };
int K = 2 , M = 3 ;
int N = arr.length;
sortCircularArray(arr, N, K, M);
for ( int i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
}
|
Python3
def sortCircularArray(arr, n, k, m):
tempArr = [ 0 ] * m
for i in range (m):
tempArr[i] = arr[(k + i) % n]
tempArr.sort()
for i in range (m):
arr[(k + i) % n] = tempArr[i]
if k + m > n:
for i in range (m, n - k):
arr[(k + i) % n] = tempArr[i - m]
if __name__ = = '__main__' :
arr = [ 4 , 1 , 6 , 5 , 3 ]
K = 2
M = 3
N = len (arr)
sortCircularArray(arr, N, K, M)
for i in range (N):
print (arr[i], end = " " )
|
C#
using System;
public class GFG {
public static void SortCircularArray( int [] arr, int n,
int k, int m)
{
int [] tempArr = new int [m];
for ( int i = 0; i < m; i++) {
tempArr[i] = arr[(k + i) % n];
}
Array.Sort(tempArr);
for ( int i = 0; i < m; i++) {
arr[(k + i) % n] = tempArr[i];
}
if (k + m > n) {
for ( int i = m; i < n - k; i++) {
arr[(k + i) % n] = tempArr[i - m];
}
}
}
public static void Main()
{
int [] arr = { 4, 1, 6, 5, 3 };
int K = 2, M = 3;
int N = arr.Length;
SortCircularArray(arr, N, K, M);
for ( int i = 0; i < N; i++) {
Console.Write(arr[i] + " " );
}
Console.WriteLine();
}
}
|
Javascript
function sortCircularArray(arr, n, k, m) {
let tempArr = [];
for (let i = 0; i < m; i++) {
tempArr[i] = arr[(k + i) % n];
}
tempArr.sort((a, b) => a - b);
for (let i = 0; i < m; i++) {
arr[(k + i) % n] = tempArr[i];
}
if (k + m > n) {
for (let i = m; i < n - k; i++) {
arr[(k + i) % n] = tempArr[i - m];
}
}
}
let arr = [4, 1, 6, 5, 3];
let K = 2, M = 3;
let N = arr.length;
sortCircularArray(arr, N, K, M);
for (let i = 0; i < N; i++) {
console.log(arr[i] + " " );
}
|
Time Complexity: O(M log M), where M is the number of elements to be sorted.
Auxiliary Space: O(M), since we are creating a new array of size M to store the M elements to be sorted.
Last Updated :
01 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...