Advertisement
dmilicev

sort_four_numbers_with_names_without_arrays_and_loops_v1.c

Nov 12th, 2020
1,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. /*
  2.  
  3.     sort_four_numbers_with_names_without_arrays_and_loops_v1.c
  4.  
  5. A nice way to do small, fixed-size sorts is using a sorting network:
  6. https://en.wikipedia.org/wiki/Sorting_network
  7.  
  8. int tmp;
  9. if (a > b) { tmp = a; a = b; b = tmp; }
  10. if (c > d) { tmp = c; c = d; d = tmp; }
  11. if (a > c) { tmp = a; a = c; c = tmp; }
  12. if (b > d) { tmp = b; b = d; d = tmp; }
  13. if (b > c) { tmp = b; b = c; c = tmp; }
  14.  
  15. Each line codes a comparison and swap between two elements.
  16. You can use this page to generate optimal sorting networks for small numbers of inputs.
  17. http://pages.ripco.net/~jgamble/nw.html
  18. To sort in reverse order, just flip the > signs to < signs.
  19.  
  20.  
  21.     You can find all my C programs at Dragan Milicev's pastebin:
  22.  
  23.     https://pastebin.com/u/dmilicev
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28.  
  29. struct Number_and_name
  30. {
  31.     int num;
  32.     char name[10];
  33. };
  34.  
  35. // To sort in decrease order, just flip the > signs to < signs.
  36. void sort_four_numbers_without_arrays_and_loops( struct Number_and_name *a,
  37.                                                  struct Number_and_name *b,
  38.                                                  struct Number_and_name *c,
  39.                                                  struct Number_and_name *d )
  40. {
  41.     struct Number_and_name tmp;
  42.  
  43.     if ( (*a).num > (*b).num )              // (*a).num  is same as  a->num
  44.     {
  45.         tmp = *a;
  46.         *a = *b;
  47.         *b = tmp;
  48.         printf("\n %d  %d  %d  %d \n", (*a).num, (*b).num, (*c).num, (*d).num );
  49.     }
  50.  
  51.     if ((*c).num > (*d).num)
  52.     {
  53.         tmp = *c;
  54.         *c = *d;
  55.         *d = tmp;
  56.         printf("\n %d  %d  %d  %d \n", (*a).num, (*b).num, (*c).num, (*d).num );
  57.     }
  58.  
  59.     if ((*a).num > (*c).num)
  60.     {
  61.         tmp = *a;
  62.         *a = *c;
  63.         *c = tmp;
  64.         printf("\n %d  %d  %d  %d \n", (*a).num, (*b).num, (*c).num, (*d).num );
  65.     }
  66.  
  67.     if ((*b).num > (*d).num)
  68.     {
  69.         tmp = *b;
  70.         *b = *d;
  71.         *d = tmp;
  72.         printf("\n %d  %d  %d  %d \n", (*a).num, (*b).num, (*c).num, (*d).num );
  73.     }
  74.  
  75.     if ((*b).num > (*c).num)
  76.     {
  77.         tmp = *b;
  78.         *b = *c;
  79.         *c = tmp;
  80.         printf("\n %d  %d  %d  %d \n", (*a).num, (*b).num, (*c).num, (*d).num );
  81.     }
  82. }
  83.  
  84.  
  85. int main(void)
  86. {
  87.     struct Number_and_name a, b, c, d;
  88.  
  89.     a.num = 4;
  90.     sprintf( a.name, "first" );
  91.  
  92.     b.num = 3;
  93.     sprintf( b.name, "second" );
  94.  
  95.     c.num = 2;
  96.     sprintf( c.name, "third" );
  97.  
  98.     d.num = 1;
  99.     sprintf( d.name, "fourth" );
  100.  
  101.     printf("\n   a \t  b \t  c \t d \n   %d \t  %d \t  %d \t %d \n %s  %s  %s  %s \n",
  102.             a.num, b.num, c.num, d.num,
  103.             a.name, b.name, c.name, d.name );
  104.  
  105.     sort_four_numbers_without_arrays_and_loops( &a, &b, &c, &d );
  106.  
  107.     printf("\n   a \t  b \t  c \t d \n   %d \t  %d \t  %d \t %d \n %s  %s  %s  %s \n",
  108.             a.num, b.num, c.num, d.num,
  109.             a.name, b.name, c.name, d.name );
  110.  
  111.  
  112.     return 0;
  113.  
  114. } // main()
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement