Sort decreasing permutation of N using triple swaps | Function to sort array ; The three indices that has to be chosen ; Check if possible to sort array ; Swapping to bring element at required position Bringing at least one element at correct position ; Tracing changes in array ; Print the sorted array ; If not possible to sort ; Driver Code
function sortArray ( A , N ) { let x = 0 , y = 0 , z = 0 ; if ( N % 4 == 0 N % 4 == 1 ) { for ( let i = 0 ; i < N / 2 ; i ++ ) { x = i ; if ( i % 2 == 0 ) { y = N - i - 2 ; z = N - i - 1 ; } A [ z ] = A [ y ] ; A [ y ] = A [ x ] ; A [ x ] = x + 1 ; } document . write ( " " ) ; for ( let i = 0 ; i < N ; i ++ ) document . write ( A [ i ] + " " ) ; } else { document . write ( " " ) ; } } let A = [ 5 , 4 , 3 , 2 , 1 ] ; let N = A . length ; sortArray ( A , N ) ;