Recursive function doesn't work in openBOR!

White Dragon

New member
This is one of major bugs in openBOR.
The recursive funtions dont work!!
For an example I tried to implement the quicksort algorithm and the recursive func doesnt work.
Instead the iterative version works well.
DC this is a very very bad thing for this engine script  :'( :'( :'( :'( :'(
 
I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes my say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say I hate recursive problem solving so much it makes me say...
 
But seriously, while recursion is not specifically supported, there shouldn't be any reason you can't do it with a wrapper function. Post an example of your code and let's have a look.

DC
 
;D ;D ;D
Recursivity is important in a script.

However this is a not working recursive quicksort (in openbor):
Code:
void quicksort(void list1, void list2, void list3, int l, int r, int order_flag) {
   int j;

   if ( order_flag == NULL() ) order_flag = 0;
   if( l < r )  {
       // divide and conquer
       j = partition(list1,list2,list3, l, r, order_flag);
       quicksort(list1,list2,list3, l, j-1, order_flag);
       quicksort(list1,list2,list3, j+1, r, order_flag);
   }
}

int partition(void list1, void list2, void list3, int l, int r, int order_flag) {
   int pivot, i, j, t;

   pivot = get(list1,l);
   i = l; j = r+1;

   if ( order_flag == NULL() ) order_flag = 0;
   while(1) {
        if ( !order_flag ) { // asc
            do ++i; while (get(list1,i) <= pivot && i <= r );
            do --j; while (get(list1,j) > pivot && j > l );
        } else { // desc
            do ++i; while (get(list1,i) > pivot && i <= r );
            do --j; while (get(list1,j) <= pivot && j > l );
        }

        if( i >= j ) break;

        swap(list1,list2,list3,i,j);
   }
   swap(list1,list2,list3,l,j);

   return j;
}

void swap(void list1, void list2, void list3, int i, int j) {
    int t;
    char t_str2, t_str3;

    t = get(list1,i); t_str2 = get(list2,i); t_str3 = get(list3,i);
    set(list1,i,get(list1,j)); set(list2,i,get(list2,j)); set(list3,i,get(list3,j));
    set(list1,j,t); set(list2,j,t_str2); set(list3,j,t_str3);
}

This is a working iterative quicksort (in openbor):
Code:
void swap(void list1, void list2, void list3, int i, int j) {
    int t;
    char t_str2, t_str3;

    t = get(list1,i); t_str2 = get(list2,i); t_str3 = get(list3,i);
    set(list1,i,get(list1,j)); set(list2,i,get(list2,j)); set(list3,i,get(list3,j));
    set(list1,j,t); set(list2,j,t_str2); set(list3,j,t_str3);
}

int partition(void list1, void list2, void list3, int l, int r, int order_flag) {
    int x = get(list1,r);
    int i = (l - 1), j;

    for (j = l; j <= r-1; j++) {
        if ( !order_flag ) {
            if (get(list1,j) <= x) {
                i++;
                swap(list1,list2,list3,i,j);
            }
        } else {
            if (get(list1,j) > x) {
                i++;
                swap(list1,list2,list3,i,j);
            }
        }
    }
    swap(list1,list2,list3,i+1,r);

    return (i+1);
}

void quickSortIterative(void list1, void list2, void list3, int l, int r, int order_flag) {
    // Create an auxiliary stack
    int stack = array(r-l+1);

    // initialize top of stack
    int top = -1;

    // push initial values of l and h to stack
    set(stack, ++top, l);
    set(stack, ++top, r);

    if ( order_flag == NULL() ) order_flag = 0;

    // Keep popping from stack while is not empty
    while ( top >= 0 ) {
        // Pop h and l
        r = get(stack, top--);
        l = get(stack, top--);

        // Set pivot element at its correct position in sorted array
        int p = partition(list1, list2, list3, l, r, order_flag);

        // If there are elements on left side of pivot, then push left
        // side to stack
        if ( p-1 > l ) {
            set(stack, ++top, l);
            set(stack, ++top, p-1);
        }

        // If there are elements on right side of pivot, then push right
        // side to stack
        if ( p+1 < r ) {
            set(stack, ++top, p+1);
            set(stack, ++top, r);
        }
    }

    free(stack);
}
 
nice quicksort!
by the way did you have done a bubblesort too ?

last time i did one i was at university hehe
 
Yeah, recursion isn't supported in OpenBOR because of the way the script engine is implemented.  It's actually a useful memory-saving trick.  As useful as recursion is, it's probably worth changing the script engine to support it.
 
Back
Top Bottom