Small Triangles, Large Triangles

  • + 2 comments

    Solution using bubble sort

    void swap(triangle *trA, triangle *trB) {
        triangle temp = *trA;
        *trA = *trB;
        *trB = temp;
    }
    
    float getArea(triangle tr) {
        float p = ((float)tr.a + (float)tr.b + (float)tr.c) / 2;
        return sqrt(p * (p - (float)tr.a) * (p - (float)tr.b) * (p - (float)tr.c));
    }
    void sort_by_area(triangle* tr, int n) {
        for (int i = 0; i < n - 1; i++) 
            for (int j = 0; j < n - i - 1; j++)
                if (getArea(*(tr + j)) > getArea(*(tr + j + 1)))
                    swap(tr + j, tr + j + 1);
    }
    
    • + 1 comment

      Agh, the type casting for p! Got me.

      • + 0 comments

        got me the first time too :)

    • + 0 comments

      Prefer a faster sorting algorithm like quicksort or heapsort