Algorithm to rotate an array in linear time

后端 未结 22 1915
我寻月下人不归
我寻月下人不归 2020-11-28 05:05

How to rotate an integer array by i times using swap function only in linear time.

相关标签:
22条回答
  • 2020-11-28 06:00

    An O(1) method of accomplishing this, in python:

    class OffsetList(list):
        __slots__ = 'offset'
        def __init__(self, init=[], offset=-1):
            super(OffsetList, self).__init__(init)
            self.offset = offset
        def __getitem__(self, key):
            return super(OffsetList, self).__getitem__(key + self.offset)
        def __setitem__(self, key, value):
            return super(OffsetList, self).__setitem__(key + self.offset, value)
        def __delitem__(self, key):
            return super(OffsetList, self).__delitem__(key + self.offset)
        def index(self, *args):
            return super(OffsetList, self).index(*args) - self.offset
    

    This is based on this answer about using a 1-based list in python.

    This does have the slight glitch that if you attempt to index an item off the end of the list, it will return items from the (new) beginning, and negative indicies less than the size minus the offset won't work.

    0 讨论(0)
  • 2020-11-28 06:01
    public int[] shift(int[] A, int K) {
        int N = A.length;
        if (N == 0)
            return A;
        int mid =  -K % N;
        if (mid < 0)
            mid += N;
        if (mid == 0)
            return A;
    
        reverseSubArray(A, 0 , mid - 1);
    
        reverseSubArray(A, mid , N - 1);
    
        reverseSubArray(A, 0 , N - 1);
    
        return A;
    }
    
    private void reverseSubArray(int[] A, int start , int end){
        int i = 0;
        int tmp;
        while (i < (end - start + 1) / 2) {
            tmp = A[i + start];
            A[i + start] = A[end - i];
            A[end - i] = tmp;
            i++;
        }
    
    }
    

    Doug McIlroy’s Handwaving Description

    0 讨论(0)
  • 2020-11-28 06:01

    why only swap function?

    O(n) in time and space:

    var rotateCount = 1;
    var arr = new Array(1,2,3,4,5,6,7,8,9,10);
    
    tmp = new Array(arr.length);
    for (var i = 0; i<arr.length; i++)
        tmp[(i+rotateCount)%arr.length]=arr[i];
    arr = tmp;
    
    alert(arr);
    
    0 讨论(0)
  • 2020-11-28 06:02

    For circular right rotation.

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt() % n;
        int q = scan.nextInt();
        int arr[] = new int[n];
    
        for (int i = 0; i < n; i++) 
        {
            int a = i + k;
            int pos = (a < n) ? a : a - n;
            arr[pos] = scan.nextInt();
        }
        for (int j = 0; j < q; j++) 
        {
            System.out.println(arr[scan.nextInt()]);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:03

    Here's a better solution, of a different nature than the others. It involves fewer array swaps than the others. Python:

    import fractions
    # rotates an array in-place i positions to the left, in linear time
    def rotate(arr,i):
        n = len(arr)
        reps = fractions.gcd(n,i)
        swaps = n / reps
        for start in xrange(reps):
            ix = start
            tmp = arr[ix]
            for s in xrange(swaps-1):
                previx = ix
                ix = (ix + i) % n
                arr[previx] = arr[ix]
            arr[ix] = tmp
        return arr
    
    0 讨论(0)
  • 2020-11-28 06:03

    here is my answer using js hope this helps where k is the number of the rotations you want to preform

     var arrayRoatate=function(array,k){
        for(;k>0;k--) {
         var nextElementValue=undefined;
            for (var i = 0; i < array.length; i=i+2) {
                var nextElement = i + 1;
                if (nextElement >= array.length)
                    nextElement = nextElement - array.length;
                var tmp=array[i];
                if(nextElementValue!==undefined)
                    array[i]=nextElementValue
                nextElementValue=array[nextElement];
                array[nextElement]=tmp;
    
            }
        }
    return array;
    
    0 讨论(0)
提交回复
热议问题