Sort M elements of given circular array starting from index K | C ++ program for the above approach ; Function to print the circular array ; Print the array ; Function to sort m elements of diven circular array starting from index k ; Traverse M elements ; Iterate from index k to k + m - 1 ; Check if the next element in the circular array is less than the current element ; Swap current element with the next element ; Print the circular array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }