Thread: Divide & conquer find min and max by recurse

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36

    Divide & conquer find min and max by recurse

    In this code nothing modify except function minMaxSearchRecursive

    Code:
    int min(int a, int b) {   
     if (a < b) {
            return a;
        } else {
            return b;
        }
    }
    
    
    int max(int a, int b) {
        if (a < b) {
            return b;
        } else {
            return a;
        }
    }
    
    
    typedef struct tuple {
        int min;
        int max;
    } Tuple;
    
    
    /**
     * Create duo value min and max, auxiliary function is already implemented for you
     */
    Tuple createTuple(int min, int max);
    
    
    /**
     * Function search values min and max in array 'array' help by
      * divide and conquer algorythm.
     * In array is searching in range from index 'left' to index 'right'
     */
    Tuple minMaxSearchRecursive(int *array, int left, int right) {
        int halfString = 0, tmpMax = 0, tmpMin = 0;
        if(left == right){
            return createTuple(array[left], array[right]);
        }else if(right == (left + 1)){
            if(array[left] > array[right]){
                return createTuple(array[right], array[left]);
            }else
                return createTuple(array[left], array[right]);
        }/*here*/else{
            halfString = (left + right) / 2;
            minMaxSearchRecursive(array, left, halfString);
            minMaxSearchRecursive(array, halfString + 1, right);
    
    
          //  return createTuple(array[tmpMax], array[tmpMin]);
        }
    }
    /*Olny in this function help. For 1 or 2 value in array work correctly. But the last  *here*ELSE incorrectly. 
    I don't know why using Tuple function and not int function. And how return minimum and maximum.*/
    
    // test function, what is writed in printf is not important
    
    void testMinMaxSearchRecursive() {
        printf("\nTest 16. rekurzivni vyhledavani minima a maxima v poli [1]: ");
        int array1[1] = {1};
        Tuple ret = minMaxSearchRecursive(array1, 0, 0);
        if (ret.min == 1 && ret.max == 1) {
            printf("OK\n");
        } else {
            printf("NOK, v poli [1] je min 1 a max 1,");
            printf(" vracite (%d, %d) != (1, 1)\n", ret.min, ret.max);
        }
    
    
        printf("Test 17. rekurzivni vyhledavani minima a maxima v poli [2, 1]: ");
        int array2[2] = {2, 1};
        ret = minMaxSearchRecursive(array2, 0, 1);
        if (ret.min == 1 && ret.max == 2) {
            printf("OK\n");
        } else {
            printf("NOK, v poli [2, 1] je min 1 a max 2,"),
            printf(" vracite (%d, %d) != (1, 2)\n", ret.min, ret.max);
        }
    
    
        printf("Test 18. rekurzivni vyhledavani minima a maxima v poli [0..99]: ");
        int array3[100];
        for (int i = 0; i < 100; ++i)
            array3[i] = (int) i;
    
    
        ret = minMaxSearchRecursive(array3, 0, 99);
        if (ret.min == 0 && ret.max == 99) {
            printf("OK\n");
        } else {
            printf("NOK, v poli [0..99] je min 0 a max 99,");
            printf(" vracite (%d, %d) != (0, 99)\n", ret.min, ret.max);
        }
    
    
        printf("Test 19. rekurzivni vyhledavani minima a maxima v poli nahodnych cisel: ");
        int array4[100];
        for (int i = 0; i < 100; ++i)
            array4[i] = rand() % 1000 + 1;
        array4[21] = 0;
        array4[45] = 1001;
        ret = minMaxSearchRecursive(array4, 0, 99);
        if (ret.min == 0 && ret.max == 1001) {
            printf("OK\n");
        } else {
            printf("NOK, v poli je min 0 a max 1001,");
            printf(" vracite (%d, %d) != (0, 1001)\n", ret.min, ret.max);
        }
    
    
        printf("Test 20. rekurzivni vyhledavani minima a maxima v poli nahodnych cisel (opakujicise minimum a maximum): ");
        int array5[100];
        for (int i = 0; i < 100; ++i)
            array5[i] = rand() % 1000 + 1;
        array5[21] = 0;
        array5[61] = 0;
        array5[42] = 1001;
        array5[45] = 1001;
        ret = minMaxSearchRecursive(array5, 0, 99);
        if (ret.min == 0 && ret.max == 1001) {
            printf("OK\n");
        } else {
            printf("NOK, v poli je min 0 a max 1001,");
            printf(" vracite (%d, %d) != (0, 1001)\n", ret.min, ret.max);
        }
    }
    Last edited by Siggi; 03-18-2015 at 05:36 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > minMaxSearchRecursive(array, left, halfString);
    > minMaxSearchRecursive(array, halfString + 1, right);
    Both of these return a Tuple (which you need to do something with).
    Say store in local variables.

    Then you choose which one to return.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    But how i save Tuple in recurse when i can save only one value and not pair value min and max?

    I tried: Tuple min = *recurse*; but it save one value and incorrect
    or: createTuple(min,max) or createTuple(array[min], array[max]) and createTuple(array[tmpMin], array[tmpMax]) but all failed.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Tuple a = minMaxSearchRecursive(array, left, halfString);
    Tuple b = minMaxSearchRecursive(array, halfString + 1, right);

    Now do something to do either
    return a;

    or
    return b;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    But i said, Tuple min or a = recurse; returning wrong value, it may return 0 but it returning 526.

    Code:
            Tuple tmpA = minMaxSearchRecursive(array, left, halfString);
            Tuple tmpB = minMaxSearchRecursive(array, halfString + 1, right);
          /*  printf("\n**%d**//**%d**\n", tmpA, tmpB);*/ checking values */
    divide and conquer is algorythm, which for first bisected into two parts array, count from each array min and max so there will be twice min and max and then combined into one minimum and maximum, so there is two recurse, first from left (first value) to mid (half array) and second value from mid to right (last value). Tuple doesn't support chars like <, >, == etc... problem is i don't know how get two values from one recurse and then from second recurse and compare which min and max is higher or lower.

  6. #6
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    Nobody know? :/

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What else do you want?

    You need to create a new Tuple out of the two return results.

    It doesn't seem that hard to do, if you've done the rest for yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    I don't know how work with Tuple function. All what i writed, writed to me error.

    All functions are in INT but main function is in Tuple. Try write to me instructions how continue.

    You need to create a new Tuple out of the two return results. <- i am now on monkey logic, for me its to hard.

    tmpC = tmpA + tmpB didn't work bcs Tuple doesn't support plus or if ( tmpA < tmpB ) didn't work

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what about this, for half an answer?
    min(a.min,b.min)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2014
    Location
    Czech Republic
    Posts
    36
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in divide and conquer approach to find max-min
    By ashutosh124 in forum C Programming
    Replies: 24
    Last Post: 12-28-2012, 11:31 AM
  2. Divide and Conquer Merge_sort
    By nimitzhunter in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 06:39 PM
  3. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  4. divide and conquer
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-13-2002, 09:52 AM