Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- sort_four_numbers_without_arrays_and_loops_v1.c
- A nice way to do small, fixed-size sorts is using a sorting network:
- https://en.wikipedia.org/wiki/Sorting_network
- int tmp;
- if (a > b) { tmp = a; a = b; b = tmp; }
- if (c > d) { tmp = c; c = d; d = tmp; }
- if (a > c) { tmp = a; a = c; c = tmp; }
- if (b > d) { tmp = b; b = d; d = tmp; }
- if (b > c) { tmp = b; b = c; c = tmp; }
- Each line codes a comparison and swap between two elements.
- You can use this page to generate optimal sorting networks for small numbers of inputs.
- http://pages.ripco.net/~jgamble/nw.html
- To sort in reverse order, just flip the > signs to < signs.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // To sort in decrease order, just flip the > signs to < signs.
- void sort_four_numbers_without_arrays_and_loops( int *a, int *b, int *c, int *d )
- {
- int tmp;
- if (*a > *b) { tmp = *a; *a = *b; *b = tmp; }
- if (*c > *d) { tmp = *c; *c = *d; *d = tmp; }
- if (*a > *c) { tmp = *a; *a = *c; *c = tmp; }
- if (*b > *d) { tmp = *b; *b = *d; *d = tmp; }
- if (*b > *c) { tmp = *b; *b = *c; *c = tmp; }
- }
- int main(void)
- {
- int a=2, b=1, c=4, d=3;
- printf("\n a b c d \n %d %d %d %d \n", a, b, c, d );
- sort_four_numbers_without_arrays_and_loops( &a, &b, &c, &d );
- printf("\n a b c d \n %d %d %d %d \n", a, b, c, d );
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement