HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Finding the order of sorting of an array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingorderthearrayfinding

Problem

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 
#include 

// 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  array[i - 1])
            {
                order = 1;
            }
            else if (array[i]  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", &len);
    int* input = malloc(len * sizeof(*input));
    int i;
    for (i = 0; i < len; i++)
    {
        scanf("%d", &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.

Solution

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>b) current=DEC; else if (ab) return NONE; break;
        case DEC:  if (a<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>b) current=DEC; else if (ab) return MIXED; break;
        case DEC:  if (a<b) return MIXED; break;
return current;

Code Snippets

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

Context

StackExchange Code Review Q#101427, answer score: 4

Revisions (0)

No revisions yet.