0

Is there a more efficient way to do what I have done here? Maybe without having to use so many if statements.

//This function takes the references of the values inputed and sorts them
//in ascending order without returning anything.
void sortFunction(int *fptrOne, int *fptrTwo, int *fptrThree){

//Variables to hold max min and mid values
int max, min, mid;

//Series of if statements to determine max min and mid values.
if(*fptrOne < *fptrTwo && *fptrOne < *fptrThree)
    min = *fptrOne;
    else if(*fptrTwo < *fptrOne && *fptrTwo < *fptrThree)
        min = *fptrTwo;
        else if (*fptrThree < *fptrOne && *fptrThree < *fptrTwo)
            min= *fptrThree;

if(*fptrOne > *fptrTwo && *fptrOne > *fptrThree)
    max = *fptrOne;
    else if(*fptrTwo > *fptrOne && *fptrTwo > *fptrThree)
        max = *fptrTwo;
        else if (*fptrThree > *fptrOne && *fptrThree > *fptrTwo)
            max = *fptrThree;

if(*fptrOne != max && *fptrOne != min)
    mid = *fptrOne;
    else if(*fptrTwo != max && *fptrTwo != min)
        mid = *fptrTwo;
        else if(*fptrThree != max && *fptrThree != min)
            mid = *fptrThree;

//Assign min mid and max to pointers in ascending order
*fptrOne = min;
*fptrTwo = mid;
*fptrThree = max;

}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680

3 Answers3

1

Use a sorting network.

void sortFunction(int *fptrOne, int *fptrTwo, int *fptrThree)
{
    int x = *fptrOne, y = *fptrTwo, z = *fptrThree, tmp;

    if(y>z) { tmp = y; y = z; z = tmp; }
    if(x>z) { tmp = x; x = z; z = tmp; }
    if(x>y) { tmp = x; x = y; y = tmp; }

    *fptrOne = x;
    *fptrTwo = y;
    *fptrThree = z;
}
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • To swap two integers, one can alternatively use `{ a ^= b; b ^= a; a ^= b; }` block, without having to use a temporary carrier variable, i.e. `tmp` – Utkan Gezer Feb 23 '14 at 22:08
  • 2
    @ThoAppelsin: That advice might have made sense years ago but there are [good reasons to avoid XOR swap in practice](http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice). – Blastfurnace Feb 23 '14 at 22:22
1

Yes. The best (or at least easiest) way to do it is to first sort the first member, then second, then (implicitly, since first and second are sorted) last.

// I'm lazy. Swaps a and b.
void swapInt(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; }

void sortFunction(int* a, int* b, int* c)
{
    if(*a > *b) swap(a, b);
    if(*a > *c) swap(a, c); // a is now smallest
    if(*b > *c) swap(b, c);
}

There are minor optimizations that one could do here, but this is a start and should give you an idea on how to go on.

Tim Čas
  • 10,501
  • 3
  • 26
  • 32
1

You can use a simple sorting network, e.g.

void Sort2(int *p0, int *p1)
{
    if (*p0 > *p1)
    {
        int temp = *p0;
        *p0 = *p1;
        *p1 = temp;
    }
}

void Sort3(int *p0, int *p1, int *p2)
{
    Sort2(p0, p1);
    Sort2(p1, p2);
    Sort2(p0, p1);
}
Paul R
  • 208,748
  • 37
  • 389
  • 560