Skip to main content
new algorithm with O(n)
Source Link
craesh
  • 121
  • 3

C (86137 characters)

#include <stdio.h>

void rotate(int * aarray, int n, int wk) {
    whileint todo = (w--1<<n+1) {-1;
        int i = n0, j;
    int tmp = a[i-1];array[0];
     
    while (todo) {
        if (todo & 1<<i) {
            j = (i--k+n)%n;
 a[i]           array[i] = a[itodo & 1<<j ? array[j] : tmp;
            todo -1%n];= 1<<i;
        *a    i = tmp;j;
        } else tmp = array[++i];
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function ´rotate´rotate minified to 86137 characters:

void r(int*a,int n,int wk){whileint m=(w-1<<n+1)-1,i=0,j,t=a[0];while(m)if(m&1<<i){int i=n,t=a[i-1];whilej=(i--k+n)a[i]=a[i%n;a[i]=(m&1<<j)?a[j]:t;m-1%n];*a=t;=1<<i;i=j;}else t=a[++i];}

C (86 characters)

#include <stdio.h>

void rotate(int * a, int n, int w) {
    while (w--) {
        int i = n, tmp = a[i-1];
        while (i--) a[i] = a[i-1%n];
        *a = tmp;
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function ´rotate´ minified to 86 characters:

void r(int*a,int n,int w){while(w--){int i=n,t=a[i-1];while(i--)a[i]=a[i-1%n];*a=t;}}

C (137 characters)

#include <stdio.h>

void rotate(int * array, int n, int k) {
    int todo = (1<<n+1)-1;
    int i = 0, j;
    int tmp = array[0];
     
    while (todo) {
        if (todo & 1<<i) {
            j = (i-k+n)%n;
            array[i] = todo & 1<<j ? array[j] : tmp;
            todo -= 1<<i;
            i = j;
        } else tmp = array[++i];
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function rotate minified to 137 characters:

void r(int*a,int n,int k){int m=(1<<n+1)-1,i=0,j,t=a[0];while(m)if(m&1<<i){j=(i-k+n)%n;a[i]=(m&1<<j)?a[j]:t;m-=1<<i;i=j;}else t=a[++i];}
reduced even more, now 86 characters
Source Link
craesh
  • 121
  • 3

C (9686 characters)

#include <stdio.h>

void rotate(int * a, int n, int w) {
    int i, tmp;
    while (w--) {
        int i = n;
       n, tmp = a[i-1];
        while (i--) a[i] = a[i-1%n];
        *a = tmp;
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function ´rotate´ minified to 9686 characters:

void r(int*a,int n,int w){int i,tmp;whilewhile(w--){i=n;tmp=a[iint i=n,t=a[i-1];while(i--)a[i]=a[i-1%n];*a=tmp;1%n];*a=t;}}

C (96 characters)

#include <stdio.h>

void rotate(int * a, int n, int w) {
    int i, tmp;
    while (w--) {
        i = n;
        tmp = a[i-1];
        while (i--) a[i] = a[i-1%n];
        *a = tmp;
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function ´rotate´ minified to 96 characters:

void r(int*a,int n,int w){int i,tmp;while(w--){i=n;tmp=a[i-1];while(i--)a[i]=a[i-1%n];*a=tmp;}}

C (86 characters)

#include <stdio.h>

void rotate(int * a, int n, int w) {
    while (w--) {
        int i = n, tmp = a[i-1];
        while (i--) a[i] = a[i-1%n];
        *a = tmp;
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function ´rotate´ minified to 86 characters:

void r(int*a,int n,int w){while(w--){int i=n,t=a[i-1];while(i--)a[i]=a[i-1%n];*a=t;}}
Source Link
craesh
  • 121
  • 3

C (96 characters)

#include <stdio.h>

void rotate(int * a, int n, int w) {
    int i, tmp;
    while (w--) {
        i = n;
        tmp = a[i-1];
        while (i--) a[i] = a[i-1%n];
        *a = tmp;
    }
}

int main() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    rotate(a, 9, 4);
    for (int i=0; i<9;i++) printf("%d ", a[i]);
    printf("\n");
}

Function ´rotate´ minified to 96 characters:

void r(int*a,int n,int w){int i,tmp;while(w--){i=n;tmp=a[i-1];while(i--)a[i]=a[i-1%n];*a=tmp;}}