introduction to algorithms:exercise 2.3-2
as request in this exercise, we won't use sentinels any more.
here we go, pseudocode followed;
merge(A,p,q,r)
__n1 = q-p+1; //note the trivial operation with the indices;
__n2 = r-q; //concern with how we value q;
__L = malloc(n1);//we just need the proper memory for we won't use sentinels any more;
__R = malloc(n2);
__for ( i = 0, j = 0, k = 0; i < n1 && j < n2; ) {
____if ( L[i] <= R[j] )
______A[k++] = L[i++];
____else
______A[k++] = R[j++];
__}
__if ( i == n1 && j != n2 ) {
____for ( ; j < n2; j++,k++ ) {
______A[k] = R[j];
____}
__}
__if ( i != n1 && j == n2 ) {
____for ( ; i < n1; i++,k++ ) {
______A[k] = L[i];
____}
__}
__free(L);
__free(R);
merge end;
that's all.
here we go, pseudocode followed;
merge(A,p,q,r)
__n1 = q-p+1; //note the trivial operation with the indices;
__n2 = r-q; //concern with how we value q;
__L = malloc(n1);//we just need the proper memory for we won't use sentinels any more;
__R = malloc(n2);
__for ( i = 0, j = 0, k = 0; i < n1 && j < n2; ) {
____if ( L[i] <= R[j] )
______A[k++] = L[i++];
____else
______A[k++] = R[j++];
__}
__if ( i == n1 && j != n2 ) {
____for ( ; j < n2; j++,k++ ) {
______A[k] = R[j];
____}
__}
__if ( i != n1 && j == n2 ) {
____for ( ; i < n1; i++,k++ ) {
______A[k] = L[i];
____}
__}
__free(L);
__free(R);
merge end;
that's all.
0 Comments:
Post a Comment
<< Home