title: Sorting sides of n triangles according to the area
body: <p>Given n triangles, with sides a,b,c print them in the same style by sorting them from the smallest to the largest.
  Complete problem: <a href="https://www.hackerrank.com/challenges/small-triangles-large-triangles/problem" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/small-triangles-large-triangles/problem</a></p>
  
  <p>In the solution, we have a structure named as Triangle. It has 3 integers a,b,c. An array of Triangle is made, named as tr and the input is passed to the function sort_by_area.
  My approach is to apply bubble sort on this array.But instead of comparing tr[j] > tr[j+1] like we do in normal bubble sort, I'm comparing the areas of tr[j] and tr[j+1]. Now, if area of tr[j] > tr[j+1]: Swap.</p>
  
  <p>The problem: In the end the results are wrong. The array doesn't sort properly. First I thought it's a typo somewhere so I rewrite the code but the problem persists.</p>
  
  <pre><code>double area (int a, int b, int c)
  {
   double  p = (a+b+c)/2;
   return sqrt(p*(p-a)*(p-b)*(p-c));
  }
  
  void sort_by_area(triangle* tr, int n) {
  /**
  * Sort an array a of the length n
  */
  int i,j;
  double area1, area2;
  triangle temp;
  for(i = 0; i &lt; n-1;++i)
  { 
      for(j = 0; j &lt; n-i-1; ++j)
      {
          area1 = area(tr[j].a , tr[j].b, tr[j].c);
          area2 = area(tr[j+1].a , tr[j+1].b, tr[j+1].c);
          if(area1 &gt; area2)
          {
              temp = tr[j];
              tr[j] = tr[j+1];
              tr[j+1] = temp;
          } 
      }
  }
  
  }
  </code></pre>
  
  <p>input: 20</p>
  
  <p>23 37 47
  22 18 5
  58 31 31
  28 36 40
  54 62 11
  31 41 14
  53 18 54
  41 38 55
  55 44 44
  44 48 18
  26 41 65
  20 23 21
  58 61 50
  28 56 56
  20 39 32
  33 45 49
  26 41 62
  31 46 39
  48 49 67
  57 33 45</p>
  
  <p>expected output:
  22 18 5
  31 41 14
  20 23 21
  54 62 11
  26 41 65
  58 31 31
  20 39 32
  26 41 62
  44 48 18
  23 37 47
  53 18 54
  28 36 40
  31 46 39
  33 45 49
  57 33 45
  28 56 56
  41 38 55
  55 44 44
  48 49 67
  58 61 50</p>
  
  <p>actual output:
  22 18 5
  54 62 11
  31 41 14
  20 23 21
  26 41 65
  20 39 32
  58 31 31
  26 41 62
  23 37 47
  44 48 18
  53 18 54
  28 36 40
  31 46 39
  33 45 49
  57 33 45
  28 56 56
  41 38 55
  55 44 44
  48 49 67
  58 61 50</p>