Skip to content

What is the most efficient c program for sorting 3 values using if-else?

An answer to this question on Stack Overflow.

Question

Let us consider that I need to develop a program that reads 3 values and that prints these values in ascending order, using only if-else structures.

Notice that I know the classical sorting algorithms. But the point here is how to develop a sorting algorithm for 3 values using simple conditional structures.

I have implemented 2 versions. I need to identify which one is the most officient and why. Let us consider efficiency inversely proportional to the amount of time taken by the program.

I think that one way to measure this would be to count the minimum and the maximum amount of comparisons that are necessary. That is, to evaluate the best and the worst cases. But the number of conditions in the ifs are different in the two algorithms.

Let us ignore the time taken by printf.

Version 1:

#include <stdio.h>
int main()
{
    int v1,v2,v3;
    printf("Provide 3 values:\n");
    scanf("%d%d%d",&v1,&v2,&v3);
    if ( v1 <= v2 && v1 <= v3){
        if( v2 <= v3 ){
            printf("%d, %d, %d\n", v1, v2, v3);
        }
        else{
            printf("%d, %d, %d\n", v1, v3, v2);
        }
    }
    else{
        if(v2 <= v1 && v2 <= v3){
            if(v1 <= v3){
                printf("%d, %d, %d\n", v2, v1, v3);
            }
            else{
                printf("%d, %d, %d\n", v2, v3, v1);
            }
        }
        else{
            if(v2 <= v1){
                printf("%d, %d, %d\n", v3, v2, v1);
            }
            else{
                printf("%d, %d, %d\n", v3, v1, v2);
            }
        }
    }
    return 0;
}

Version 2

#include <stdio.h>
int main()
{
    int v1,v2,v3;
    printf("Provide 3 values:\n");
    scanf("%d%d%d",&v1,&v2,&v3);
    if ( v1 <= v2){
        if( v1 <= v3 ){
            if(v2 <= v3){
                printf("%d, %d, %d\n", v1, v2, v3);
            }
            else{
                printf("%d, %d, %d\n", v1, v3, v2);
            }
        }
        else{
            printf("%d, %d, %d\n", v3, v1, v2);
        }
    }
    else{
        if(v2 <= v3){
            if(v1 <= v3){
                printf("%d, %d, %d\n", v2, v1, v3);
            }
            else{
                printf("%d, %d, %d\n", v2, v3, v1);
            }
        }
        else{
            printf("%d, %d, %d\n", v3, v2, v1);
        }
    }
    return 0;
}

Is there some other program (that uses only if-else) that is more efficient than these two?

@rcgldr Can you show the code that implement your idea?

Version 3

int main()
{
    int v1,v2,v3;
    printf("Provide 3 values:\n");
    scanf("%d%d%d",&v1,&v2,&v3);
    if(v1 <= v2){
        if(v2 <= v3){
            printf("%d, %d, %d\n", v1, v2, v3);
        }else if(v1 <= v3 ){
            printf("%d, %d, %d\n", v1, v3, v2);
        }else{
            printf("%d, %d, %d\n", v3, v1, v2);
        }
    }
    else{
        if(v1 <= v3){
            printf("%d, %d, %d\n", v2, v1, v3);
        }else if(v2 <= v3){
            printf("%d, %d, %d\n", v2, v3, v1);
        }else{
            printf("%d, %d, %d\n", v3, v2, v1);
        }
    }
    return 0;
}

Answer

Judging efficiency is difficult because it relies deeply on your CPU and your compiler's optimizer.

However, from a theoretic perspective, sorting 3 elements requires at least 3 comparisons. Sequence A036604 has values for larger numbers of elements.

Code for sorting 5 elements is shown here - you'll probably see why this kind of sorting isn't used often.