Finding the order of sorting of an array I am finding the order of sorting of an array. The code works but can it be made better especially the return values of the function findSortOrder.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

// Returns 0 for unsorted, 1 for sorted in increasing order, 2 for sorted in decreasing order
int findSortOrder(int array[], int len)
{
    // holds the sorting order of the subarray array[0...i]
    // 0 represents no information about sorting order
    // 1 represents sorting in increasing order
    // 2 represents sorting in decreasing order
    int order = 0;
    int i;
    for (i = 1; i &lt; len; i++)
    {
        if (order == 0)
        {
            if (array[i] &gt; array[i - 1])
            {
                order = 1;
            }
            else if (array[i] &lt; array[i - 1])
            {
                order = 2;
            }
        }
        else if (order == 1)
        {
            if (array[i] &lt; array[i - 1])
            {
                return 0;
            }
        }
        else
        {
            if (array[i] &gt; array[i - 1])
            {
                return 0;
            }
        }
    }
    if (order == 0 || order == 1)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}

int main()
{
    printf("Enter length of the array: ");
    int len;
    scanf("%d", &amp;len);
    int* input = malloc(len * sizeof(*input));
    int i;
    for (i = 0; i &lt; len; i++)
    {
        scanf("%d", &amp;input[i]);
    }
    int order = findSortOrder(input, len);
    switch (order)
    {
        case 0:
            printf("Unsorted\n");
            break;
        case 1:
            printf("Sorted in increasing order\n");
            break;
        case 2:
            printf("Sorted in decreasing order\n");
            break;
    }
    free(input);
    return 0;
}


Edit:
I will be using this function to merge two sorted arrays in their sorting order. So I think if no. of elements is 1 or all are equal then sort order could be returned as increasing.
The cleanest would be an enum and a switch (current) with three cases:

In each case you can then check if you stay in the current_order or if you 
switch to another or if you can return 0/1/-1.

Simplified:

current=NONE;
for()
    a=... b= ..
    switch(current)
        case NONE: if (a&gt;b) current=DEC; else if (a&lt;b) current=INC; break;
        case INC:  if (a&gt;b) return NONE; break;
        case DEC:  if (a&lt;b) return NONE; break;
return current;


I don't know if it's the right term, but I think that is a finite state machine. You could read about those to get more information; it's always good to use for parsing stuff.



From the comments:


  What should be returned if the array is one element long?


Good point. I would introduce another return value MIXED:

current=NONE;
for()
    a=... b= ..
    switch(current)
        case NONE: if (a&gt;b) current=DEC; else if (a&lt;b) current=INC; break;
        case INC:  if (a&gt;b) return MIXED; break;
        case DEC:  if (a&lt;b) return MIXED; break;
return current;