Skip to content

rotate implementation #1

Description

There is an alternative to the current reverse x 3 approach that is also in place and O(n). This alternative might be more memory access friendly as it only loops on the array once in forward order.

// rotate left version
let rotate = function ( a , i , j , k ) {
    if ( k <= 0 ) return ;
    while ( ( i += k ) + k < j ) swapranges( a , i - k , i , a , i ) ;
    swapranges( a , i , j , a , i - k ) ;
    rotate( a , j - k , j , k - ( j - i ) ) ; // tail recursion
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions