Monday, November 30, 2009

{1,5, -5, -8,2, -1,15 } -> {-5, -8, -1, 1, 5, 2,15}

每遇到一个负数, instead of exchange with the
first 正数,把前面的正数right round shift就可以了。

Index 0 1 2 3
Data 1 5 4 -5

L = 0, R = 3
right round shift from 1 to -5 就变成
-5 1 5 4

void shiftright(int* A, int L, int R) {
        int tmp=A[R],i;
        for (i=R; i>L; i--) {
                A[i] = A[i-1];
        }
        A[L] = tmp;
}


int main(){
        int a[] = {-7, -8, 9, -6, -11, -3, -5, 2, 3};
        int n, i, j= 0;
        n = sizeof(a)/sizeof(int);
        printf("array size %d\n", n);

        for(i=0; i

No comments:

Post a Comment