Skip to content

Instantly share code, notes, and snippets.

@ulvimammaadov
Created January 5, 2022 20:12
Show Gist options
  • Save ulvimammaadov/48e704c75c0718b26539b8800ac4c51a to your computer and use it in GitHub Desktop.
Save ulvimammaadov/48e704c75c0718b26539b8800ac4c51a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a,b,c;
};
typedef struct triangle triangle;
void sort_by_area(triangle* tr, int n)
{
double area[n], s,tempArea;
struct triangle temp;
int i, j, k;
for(i = 0; i < n; i++){
s = (tr[i].a + tr[i].b + tr[i].c ) / 2.0; // (a+b+c) / 2
area[i] = sqrt(s * (s - tr[i].a) * (s - tr[i].b) * (s - tr[i].c)); // heron formula
}
for(k = 0; k < n; k++){
for(j = 0; j < n - k - 1; j++){
if(area[j] > area[j + 1]){
temp = tr[j];
tr[j] = tr[j + 1];
tr[j + 1] = temp;
tempArea = area[j];
area[j] = area[j + 1];
area[j + 1] = tempArea;
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment