Related Articles
Sort M elements of given circular array starting from index K
• Last Updated : 08 Mar, 2021

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}.

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++

 // C++ program for the above approach#include using namespace std; // Function to print the circular arrayvoid printCircularArray(int arr[], int n){    // Print the array    for (int i = 0; i < n; i++) {        cout << arr[i] << " ";    }} // Function to sort m elements of diven// circular array starting from index kvoid sortCircularArray(int arr[], int n,                       int k, int m){    // Traverse M elements    for (int i = 0; i < m; i++) {         // Iterate from index k to k + m - 1        for (int j = k; j < k + m - 1; j++) {             // Check if the next element            // in the circular array is            // less than the current element            if (arr[j % n]                > arr[(j + 1) % n]) {                 // Swap current element                // with the next element                swap(arr[j % n], arr[(j + 1) % n]);            }        }    }     // Print the circular array    printCircularArray(arr, n);} // Driver Codeint main(){    int arr[] = { 4, 1, 6, 5, 3 };    int K = 2, M = 3;    int N = sizeof(arr) / sizeof(arr[0]);     // Function Call    sortCircularArray(arr, N, K, M);     return 0;}

## Java

 // Java program for the above approachclass GFG{      // Function to print the circular arraystatic void printCircularArray(int arr[], int n){    // Print the array    for (int i = 0; i < n; i++) {        System.out.print(arr[i] + " ");    }}  // Function to sort m elements of diven// circular array starting from index kstatic void sortCircularArray(int arr[], int n,                       int k, int m){    // Traverse M elements    for (int i = 0; i < m; i++) {          // Iterate from index k to k + m - 1        for (int j = k; j < k + m - 1; j++) {              // Check if the next element            // in the circular array is            // less than the current element            if (arr[j % n]                > arr[(j + 1) % n]) {                  // Swap current element                // with the next element                int t = arr[j % n];                arr[j % n] = arr[(j + 1) % n];                arr[(j + 1) % n] = t;            }        }    }      // Print the circular array    printCircularArray(arr, n);}  // Driver Codepublic static void main (String[] args){      int[] arr = { 4, 1, 6, 5, 3 };    int K = 2, M = 3;    int N = arr.length;      // Function Call    sortCircularArray(arr, N, K, M);}} // This code is contributed by susmitakundugoaldanga

## Python3

 # Python3 program for the above approach # Function to print the circular arraydef printCircularArray(arr, n):         # Print the array    for i in range(n):        print(arr[i], end = " ") # Function to sort m elements of diven# circular array starting from index kdef sortCircularArray(arr, n, k, m):     # Traverse M elements    for i in range(m):         # Iterate from index k to k + m - 1        for j in range(k, k + m - 1):             # Check if the next element            # in the circular array is            # less than the current element            if (arr[j % n] > arr[(j + 1) % n]):                 # Swap current element                # with the next element                arr[j % n], arr[(j + 1) % n] = (arr[(j + 1) % n],                                                arr[j % n])                     # Print the circular array    printCircularArray(arr, n)     # Driver Codeif __name__ == "__main__" :     arr = [ 4, 1, 6, 5, 3 ]    K = 2    M = 3    N = len(arr)         # Function Call    sortCircularArray(arr, N, K, M) # This code is contributed by AnkThon

## C#

 // C# program for the above approachusing System; class GFG{              // Function to print the circular array    static void printCircularArray(int []arr, int n)    {        // Print the array        for (int i = 0; i < n; i++)        {            Console.Write(arr[i] + " ");        }    }          // Function to sort m elements of diven    // circular array starting from index k    static void sortCircularArray(int []arr, int n,                           int k, int m)    {               // Traverse M elements        for (int i = 0; i < m; i++)        {                  // Iterate from index k to k + m - 1            for (int j = k; j < k + m - 1; j++)            {                      // Check if the next element                // in the circular array is                // less than the current element                if (arr[j % n]                    > arr[(j + 1) % n]) {                          // Swap current element                    // with the next element                    int t = arr[j % n];                    arr[j % n] = arr[(j + 1) % n];                    arr[(j + 1) % n] = t;                }            }        }              // Print the circular array        printCircularArray(arr, n);    }          // Driver Code    public static void Main (string[] args)    {          int[] arr = { 4, 1, 6, 5, 3 };        int K = 2, M = 3;        int N = arr.Length;              // Function Call        sortCircularArray(arr, N, K, M);    }} // This code is contributed by AnkThon

## Javascript



Output:

4 1 3 5 6

Time Complexity: O(N2)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up