Skip to main content
added 63 characters in body
Source Link

Your algorithm is soundworks for sequences that are strictly increasing (as @Moron points out). If efficiency is a consideration, you may wish to employ iteration instead of recursion.

int getMinimIndex (const int *const a, size_t left, size_t right)
{
    while (1)
    {
        assert(left<= right);

        // Special cases:
        // 1 If left & right are same , return 
        if (left ==right)
            return left;

        // 2 For an array of size 2, return the index of minimal element
        if (left == right - 1)
            return a[left]<=a[right]?left:right;

        // 3 If the array wasn't rotated at all, 
        if (a[left] < a[right])
            return left;


        // General case
        // Go to the middle of the array
        size_t mid = (left + right) >> 1;

        // If we stepped into the "larger" subarray, we came too far, 
        // hence search the right subpart    
        if (a[left] <= a[mid])
            left = mid;
        else
        // We're still in the "smaller" subarray, hence search left subpart
            right = mid;
    }
}

Your algorithm is sound. If efficiency is a consideration, you may wish to employ iteration instead of recursion.

int getMinimIndex (const int *const a, size_t left, size_t right)
{
    while (1)
    {
        assert(left<= right);

        // Special cases:
        // 1 If left & right are same , return 
        if (left ==right)
            return left;

        // 2 For an array of size 2, return the index of minimal element
        if (left == right - 1)
            return a[left]<=a[right]?left:right;

        // 3 If the array wasn't rotated at all, 
        if (a[left] < a[right])
            return left;


        // General case
        // Go to the middle of the array
        size_t mid = (left + right) >> 1;

        // If we stepped into the "larger" subarray, we came too far, 
        // hence search the right subpart    
        if (a[left] <= a[mid])
            left = mid;
        else
        // We're still in the "smaller" subarray, hence search left subpart
            right = mid;
    }
}

Your algorithm works for sequences that are strictly increasing (as @Moron points out). If efficiency is a consideration, you may wish to employ iteration instead of recursion.

int getMinimIndex (const int *const a, size_t left, size_t right)
{
    while (1)
    {
        assert(left<= right);

        // Special cases:
        // 1 If left & right are same , return 
        if (left ==right)
            return left;

        // 2 For an array of size 2, return the index of minimal element
        if (left == right - 1)
            return a[left]<=a[right]?left:right;

        // 3 If the array wasn't rotated at all, 
        if (a[left] < a[right])
            return left;


        // General case
        // Go to the middle of the array
        size_t mid = (left + right) >> 1;

        // If we stepped into the "larger" subarray, we came too far, 
        // hence search the right subpart    
        if (a[left] <= a[mid])
            left = mid;
        else
        // We're still in the "smaller" subarray, hence search left subpart
            right = mid;
    }
}
Source Link

Your algorithm is sound. If efficiency is a consideration, you may wish to employ iteration instead of recursion.

int getMinimIndex (const int *const a, size_t left, size_t right)
{
    while (1)
    {
        assert(left<= right);

        // Special cases:
        // 1 If left & right are same , return 
        if (left ==right)
            return left;

        // 2 For an array of size 2, return the index of minimal element
        if (left == right - 1)
            return a[left]<=a[right]?left:right;

        // 3 If the array wasn't rotated at all, 
        if (a[left] < a[right])
            return left;


        // General case
        // Go to the middle of the array
        size_t mid = (left + right) >> 1;

        // If we stepped into the "larger" subarray, we came too far, 
        // hence search the right subpart    
        if (a[left] <= a[mid])
            left = mid;
        else
        // We're still in the "smaller" subarray, hence search left subpart
            right = mid;
    }
}