Sorting five integers using bitwise or comparison operators can be achieved by first getting the highest number then the second highest then the third and so on.
Here is my code in getting the highest number:
#include <stdio.h>
int main() {
int a, b, c, d, e;
int aa, bb, cc, dd, ee;
a = 4; b = 2; c = 5; d = 1; e = 3;
aa = (a > b) ?
((a > c) ? ((a > d) ? ((a > e) ? a : e) : ((d > e) ? d : e)) :
((c > d) ? ((c > e) ? c : e) : ((d > e) ? d : e))) :
((b > c) ? ((b > d) ? ((b > e) ? b : e) : ((d > e) ? d : e)) :
((c > d) ? ((c > e) ? c : e) : ((d > e) ? d : e)));
printf("highest: %d\n", aa);
return 0;
}
I think getting the second, third, forth and fifth highest number could be possible using this method.
Is there any other way in getting the median of five integers using comparison/bitwise operators? any other combinatorial method might be valid.
By the way, I'm going to implement this algorithm in hardware.
Using combinatorial method in sorting will be fast rather than using a state machine.
UPDATE
I made a combinatorial sorting in verilog using sorting networks.
@Steve Jessop thank you for providing information.
To @David Winant and @sth for providing ideas. =)
One way to think about it is to consider the 10 comparison operations between the 5 numbers as your binary inputs. Then you have options:
Some of the possibilities will never occur, so I'm sure there's some simplification possible. For instance if (a>b) and (b>c) then (a>c) will always be true. Which will help with approach #1 and generates an error case in approach #2.
The third highest number of five integers is the median, so if you get the third highest number you are fine.