Page 1 of 1

Sorting Variables

Posted: Thu Jun 02, 2011 1:34 pm
by HappyBomberGuy
Anyone have a short code snippet or pseudocode for sorting variable values from low to high?

Re: Sorting Variables

Posted: Thu Jun 02, 2011 2:21 pm
by fll-freak
How many values?
What language?
Looking to optimize speed or code size?

Re: Sorting Variables

Posted: Thu Jun 02, 2011 2:28 pm
by rmteo1

Re: Sorting Variables

Posted: Thu Jun 02, 2011 11:59 pm
by HappyBomberGuy
Only need to sort 3 variables from low to high, these will change each time through the program so I need to sort them each time. It will be in C.

Re: Sorting Variables

Posted: Sun Jun 19, 2011 12:26 am
by Longhorn Engineer
qsort(A,sizeof(A),sizeof(int),compare);

Where A is the array that holds the variables and compare is this function.

int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

More info here. http://www.cplusplus.com/reference/clib ... lib/qsort/

Its in the stdlib of C so you should be fine.