📜  根据面积增加对三角形的边进行排序

📅  最后修改于: 2022-05-13 01:56:04.717000             🧑  作者: Mango

根据面积增加对三角形的边进行排序

给定一个包含N个三角形边的数组arr[] ,任务是根据面积的递增顺序对三角形的给定边进行排序。

例子:

方法:可以通过将三角形面积的边存储在另一个数组中来解决给定的问题,然后按存储面积的升序对数组进行排序,然后打印存储在另一个数组中的边作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to rearrange the sides of
// triangle in increasing order of area
void rearrangeTriangle(
    vector > arr, int N)
{
    // Stores the area of triangles with
    // their corresponding indices
    vector > area;
 
    for (int i = 0; i < N; i++) {
 
        // Find the area
        float a = (arr[i][0]
                   + arr[i][1]
                   + arr[i][2])
                  / 2.0;
        float Area = sqrt(abs(a * (a - arr[i][0])
                              * (a - arr[i][1])
                              * (a - arr[i][2])));
 
        area.push_back({ Area, i });
    }
 
    // Sort the area vector
    sort(area.begin(), area.end());
 
    // Resultant sides
    for (int i = 0; i < area.size(); i++) {
        cout << arr[area[i].second][0]
             << " "
             << arr[area[i].second][1]
             << " "
             << arr[area[i].second][2]
             << '\n';
    }
}
 
// Driver Code
int main()
{
    vector > arr = {
        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }
    };
    int N = arr.size();
 
    rearrangeTriangle(arr, N);
 
    return 0;
}


Python3
# python program for the above approach
import math
 
# Function to rearrange the sides of
# triangle in increasing order of area
def rearrangeTriangle(arr, N):
 
        # Stores the area of triangles with
        # their corresponding indices
    area = []
 
    for i in range(0, N):
 
                # Find the area
        a = (arr[i][0] + arr[i][1] + arr[i][2]) / 2.0
        Area = math.sqrt(
            abs(a * (a - arr[i][0]) * (a - arr[i][1]) * (a - arr[i][2])))
 
        area.append([Area, i])
 
        # Sort the area vector
    area.sort()
 
    # Resultant sides
    for i in range(0, len(area)):
        print(arr[area[i][1]][0], end=" ")
        print(arr[area[i][1]][1], end=" ")
        print(arr[area[i][1]][2])
 
# Driver Code
if __name__ == "__main__":
 
    arr = [
        [5, 3, 7], [15, 20, 4], [4, 9, 6], [8, 4, 5]
    ]
    N = len(arr)
 
    rearrangeTriangle(arr, N)
 
    # This code is contributed by rakeshsahni


C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
  // Function to rearrange the sides of
  // triangle in increasing order of area
  static void rearrangeTriangle(
    List > arr, int N)
  {
     
    // Stores the area of triangles with
    // their corresponding indices
    List > area = new List >();
 
    for (int i = 0; i < N; i++) {
 
      // Find the area
      float a = (float)(arr[i][0]
                        + arr[i][1]
                        + arr[i][2])
        / 2;
      float Area = (float)Math.Sqrt(Math.Abs(a * (a - arr[i][0])
                                             * (a - arr[i][1])
                                             * (a - arr[i][2])));
 
      area.Add(new KeyValuePair (Area, i ));
    }
 
    // Sort the area List
    area.Sort((x, y) => x.Key.CompareTo(y.Key));
 
    // Resultant sides
    for (int i = 0; i < area.Count; i++) {
      Console.WriteLine(arr[area[i].Value][0] + " "
                        + arr[area[i].Value][1]
                        + " "
                        + arr[area[i].Value][2]);
    }
  }
 
  // Driver Code
  static public void Main ()
  {
    List > arr = new List >(){
      new List(){ 5, 3, 7 },
      new List(){ 15, 20, 4 },
      new List(){ 4, 9, 6 },
      new List(){ 8, 4, 5 }
    };
    int N = arr.Count;
 
    rearrangeTriangle(arr, N);
  }
}
 
// This code is contributed
// by Shubham Singh


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the area of sides
// of triangle stored in arr[]
float findArea(vector& arr)
{
 
    // Find the semi perimeter
    float a = (arr[0]
               + arr[1]
               + arr[2])
              / 2.0;
 
    // Find area using Heron's Formula
    float Area = sqrt(abs(a * (a - arr[0])
                          * (a - arr[1])
                          * (a - arr[2])));
 
    // Return the area
    return Area;
}
 
// Comparator function to sort the given
// array of sides of triangles in
// increasing order of area
bool cmp(vector& A, vector& B)
{
    return findArea(A) <= findArea(B);
}
 
// Function to rearrange the sides of
// triangle in increasing order of area
void rearrangeTriangle(
    vector > arr, int N)
{
    // Sort the array arr[] in increasing
    // order of area
    sort(arr.begin(), arr.end(), cmp);
 
    // Resultant sides
    for (int i = 0; i < N; i++) {
        cout << arr[i][0] << " " << arr[i][1] << " "
             << arr[i][2] << '\n';
    }
}
 
// Driver Code
int main()
{
    vector > arr = {
        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }
    };
    int N = arr.size();
 
    rearrangeTriangle(arr, N);
 
    return 0;
}


Python3
# Python program for the above approach
import math
 
# Function to find the area of sides
# of triangle stored in arr[]
def findArea(arr):
 
    # Find the semi perimeter
    a = (arr[0] + arr[1] + arr[2]) / 2.0
     
    # Find area using Heron's Formula
    Area = math.sqrt(abs(a * (a - arr[0]) * (a - arr[1]) * (a - arr[2])))
     
    # Return the area
    return Area
   
# Function to rearrange the sides of
# triangle in increasing order of area
def rearrangeTriangle(arr , N):
   
  # Sort the array arr[] in increasing
  # order of area
  arr.sort(key = lambda x: (findArea(x)))
   
  # Resultant sides
  for i in range(0,N):
    print(arr[i][0], arr[i][1], arr[i][2])
              
# Driver Code
if __name__ == "__main__":
 
    arr = [[5 , 3 , 7], [15 , 20 , 4], [4 , 9 , 6], [8 , 4 , 5]]
    N = len(arr)
     
    rearrangeTriangle(arr, N)
     
    # This code is contributed by bhupenderyadav18.


输出:
5 3 7
8 4 5
4 9 6
15 20 4

时间复杂度: O(N*log N)
辅助空间: O(N)

空间优化方法:上述方法也可以在空间方面进行优化,其思想是使用比较器函数对给定数组按面积升序排序。以下是使用的比较器函数:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the area of sides
// of triangle stored in arr[]
float findArea(vector& arr)
{
 
    // Find the semi perimeter
    float a = (arr[0]
               + arr[1]
               + arr[2])
              / 2.0;
 
    // Find area using Heron's Formula
    float Area = sqrt(abs(a * (a - arr[0])
                          * (a - arr[1])
                          * (a - arr[2])));
 
    // Return the area
    return Area;
}
 
// Comparator function to sort the given
// array of sides of triangles in
// increasing order of area
bool cmp(vector& A, vector& B)
{
    return findArea(A) <= findArea(B);
}
 
// Function to rearrange the sides of
// triangle in increasing order of area
void rearrangeTriangle(
    vector > arr, int N)
{
    // Sort the array arr[] in increasing
    // order of area
    sort(arr.begin(), arr.end(), cmp);
 
    // Resultant sides
    for (int i = 0; i < N; i++) {
        cout << arr[i][0] << " " << arr[i][1] << " "
             << arr[i][2] << '\n';
    }
}
 
// Driver Code
int main()
{
    vector > arr = {
        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }
    };
    int N = arr.size();
 
    rearrangeTriangle(arr, N);
 
    return 0;
}

Python3

# Python program for the above approach
import math
 
# Function to find the area of sides
# of triangle stored in arr[]
def findArea(arr):
 
    # Find the semi perimeter
    a = (arr[0] + arr[1] + arr[2]) / 2.0
     
    # Find area using Heron's Formula
    Area = math.sqrt(abs(a * (a - arr[0]) * (a - arr[1]) * (a - arr[2])))
     
    # Return the area
    return Area
   
# Function to rearrange the sides of
# triangle in increasing order of area
def rearrangeTriangle(arr , N):
   
  # Sort the array arr[] in increasing
  # order of area
  arr.sort(key = lambda x: (findArea(x)))
   
  # Resultant sides
  for i in range(0,N):
    print(arr[i][0], arr[i][1], arr[i][2])
              
# Driver Code
if __name__ == "__main__":
 
    arr = [[5 , 3 , 7], [15 , 20 , 4], [4 , 9 , 6], [8 , 4 , 5]]
    N = len(arr)
     
    rearrangeTriangle(arr, N)
     
    # This code is contributed by bhupenderyadav18.
输出:
5 3 7
8 4 5
4 9 6
15 20 4

时间复杂度: O(N*log N)
辅助空间: O(1)