We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Small Triangles, Large Triangles
Small Triangles, Large Triangles
Sort by
recency
|
418 Discussions
|
Please Login in order to post a comment
int cmp_func(triangle* x, triangle* y){
int s1 =( ( (x->a) + (x->b) + (x->c) ) * ( -(x->a) + (x->b) + (x->c) ) * ( (x->a) - (x->b) + (x->c) ) * ( (x->a) + (x->b) - (x->c) ) );
int s2 =( ( (y->a) + (y->b) + (y->c) ) * ( -(y->a) + (y->b) + (y->c) ) * ( (y->a) - (y->b) + (y->c) ) * ( (y->a) + (y->b) - (y->c) ) );
}
void swap_triangles(triangle* a, triangle* b) { triangle temp = *a; *a = *b; *b = temp; }
void swap(struct triangle *A, struct triangle *B){ int temp;
}
void sort_by_area(triangle* tr, int n) { /** * Sort an array a of the length n */
}
void sort_by_area(triangle* tr, int n) { /** * Sort an array a of the length n */ double area = (double)malloc(n * sizeof(double)); double temp_area; triangle temp_side; double p; for (int i = 0; i < n; i++) { double p = (double) (tr[i].a + tr[i].b + tr[i].c) / 2; area[i] = sqrt(p * (p - tr[i].a) * (p - tr[i].b) * (p - tr[i].c)); }
}
Simple and easy to understand
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++) { double p1 = (double)(tr[j].a + tr[j].b + tr[j].c) / 2.0; double s1 = sqrt(p1 * (p1 - (double)tr[j].a)(p1 - (double)tr[j].b)(p1 - (double)tr[j].c)); double p2 = (double)(tr[j+1].a + tr[j+1].b + tr[j+1].c) / 2.0; double s2 = sqrt(p2 * (p2 - (double)tr[j+1].a)(p2 - (double)tr[j+1].b)(p2 - (double)tr[j+1].c)); if (s1 > s2) { triangle temp; temp = tr[j]; tr[j] = tr[j+1]; tr[j+1] = temp; } } } }