Combine base case with sort (recursion)

From the book Robert Sedgewick and Kevin Wayne Algorithms of the 4th release

In the recursion code code, the main code

if(end <= start)
    {
        return;
    }

But I checked end, never getting below the index start. only end = starthappens when only one item is left. Then why is it <=less than the operator when only one condition equal to works =?

Suppose an array is accepted a[8,5,3].

Now the midpoint is the 1st index, so after dividing

a[8,5] and a[3]

divide(a,0,1) and divide(a,2,2), merge(a,0,1,2)start is less than the end in divide(a,0,1)and start=endoccurs in a function call divide(a,2,2).

Now the middle is the 0th index.

a[8] and a[5] 

divide(a,0,0) and divide(a,1,1), merge(a,0,0,1)here in both function calls is start=endvalid only.

end start, end<start . end=start.

- , < () ?

int divide(int a[], int start, int end)
{
    int mid;

    if(end<=start)
    {
        return;
    }
    else
    {
        mid = (start+end)/2;
        divide(a, start, mid);
        divide(a, mid+1, end);
        merge(a, start, mid, end);
    }
}
+4
2

, divide , end < start. , if if (end == start).

, , divide , .

void foo(int a[]) 
{ 
    divide(a, 5, 3);
}

, == <=. <=.

:

int divide(int a[], int start, int end)
{
    int mid;

    if(end > start)
    {
        mid = (start+end)/2;
        divide(a, start, mid);
        divide(a, mid+1, end);
        merge(a, start, mid, end);
    }
}

, , - .

BTW: , , , int, . , , : void divide (.....)

+3

,

void divide(int a[], int start, int end)
{
    int mid;

    if(start < end)
    {
        mid = (start+end)/2;
        divide(a, start, mid);
        divide(a, mid+1, end);
        merge(a, start, mid, end);
    }
}
+1

Source: https://habr.com/ru/post/1658546/


All Articles