📜  递归程序查找数组的最小和最大元素

📅  最后修改于: 2021-05-05 00:39:06             🧑  作者: Mango

给定一个整数数组arr ,任务是使用递归找到该数组的最小和最大元素。

例子 :

Input: arr = {1, 4, 3, -5, -4, 8, 6};
Output: min = -5, max = 8

Input: arr = {1, 4, 45, 6, 10, -8};
Output: min = -8, max = 45

递归方法来查找数组中的Minimum元素

方法:

  1. 获取要为其找到最小值的阵列
  2. 根据以下内容递归找到最小值:
    • 从末尾递归遍历数组
    • 基本情况:如果剩余数组的长度为1,则返回唯一存在的元素,即arr [0]
      if(n == 1)
         return arr[0];
      
    • 递归调用:如果不满足基本条件,则通过从末尾传递小一号的数组(即从arr [0]到arr [n-1])来调用函数。
    • 返回语句:在每个递归调用中(基本情况除外),返回当前数组的最后一个元素(即arr [n-1])和从上一个递归调用返回的元素的最小值。
      return min(arr[n-1], recursive_function(arr, n-1));
      
  3. 将递归函数返回的元素打印为最小元素

递归函数的伪代码:

If there is single element, return it.
Else return minimum of following.
    a) Last Element
    b) Value returned by recursive call
       fir n-1 elements.

下面是上述方法的实现:

C++
// Recursive C++ program to find minimum
  
#include 
using namespace std;
  
// function to print Minimum element using recursion
int findMinRec(int A[], int n)
{
    // if size = 0 means whole array has been traversed
    if (n == 1)
        return A[0];
    return min(A[n-1], findMinRec(A, n-1));
}
  
// driver code to test above function
int main()
{
    int A[] = {1, 4, 45, 6, -50, 10, 2};
    int n = sizeof(A)/sizeof(A[0]);
    cout <<  findMinRec(A, n);
    return 0;
}


Java
// Recursive Java program to find minimum
import java.util.*;
  
class GFG {
  
    // function to return minimum element using recursion
    public static int findMinRec(int A[], int n)
    {
      // if size = 0 means whole array
      // has been traversed
      if(n == 1)
        return A[0];
          
        return Math.min(A[n-1], findMinRec(A, n-1));
    }
      
    // Driver code
    public static void main(String args[])
    {
        int A[] = {1, 4, 45, 6, -50, 10, 2};
        int n = A.length;
          
        // Function calling
        System.out.println(findMinRec(A, n));
    }
}
  
//This code is contributed by Niraj_Pandey


Python3
# Recursive python 3 program to 
# find minimum
  
# function to print Minimum element 
# using recursion
def findMinRec(A, n):
      
    # if size = 0 means whole array
    # has been traversed
    if (n == 1):
        return A[0]
    return min(A[n - 1], findMinRec(A, n - 1))
  
# Driver Code
if __name__ == '__main__':
    A = [1, 4, 45, 6, -50, 10, 2]
    n = len(A)
    print(findMinRec(A, n))
  
# This code is contributed by
# Shashank_Sharma


C#
// Recursive C# program to find minimum 
using System;
  
class GFG
{
      
// function to return minimum 
// element using recursion 
public static int findMinRec(int []A,
                             int n) 
{ 
// if size = 0 means whole array 
// has been traversed 
if(n == 1) 
    return A[0]; 
      
    return Math.Min(A[n - 1],
                    findMinRec(A, n - 1)); 
} 
  
// Driver code 
static public void Main ()
{
    int []A = {1, 4, 45, 6, -50, 10, 2}; 
    int n = A.Length; 
      
    // Function calling 
    Console.WriteLine(findMinRec(A, n)); 
} 
} 
  
// This code is contributed by Sachin


PHP


C++
// Recursive C++ program to find maximum
#include 
using namespace std;
  
// function to return maximum element using recursion
int findMaxRec(int A[], int n)
{
    // if n = 0 means whole array has been traversed
    if (n == 1)
        return A[0];
    return max(A[n-1], findMaxRec(A, n-1));
}
  
// driver code to test above function
int main()
{
    int A[] = {1, 4, 45, 6, -50, 10, 2};
    int n = sizeof(A)/sizeof(A[0]);
    cout <<  findMaxRec(A, n);
    return 0;
}


Java
// Recursive Java program to find maximum
import java.util.*;
  
class GFG {
      
    // function to return maximum element using recursion
    public static int findMaxRec(int A[], int n)
    {
      // if size = 0 means whole array
      // has been traversed
      if(n == 1)
        return A[0];
          
        return Math.max(A[n-1], findMaxRec(A, n-1));
    }
  
    // Driver code
    public static void main(String args[])
    {
        int A[] = {1, 4, 45, 6, -50, 10, 2};
        int n = A.length;
          
        // Function calling
        System.out.println(findMaxRec(A, n));
    }
}
  
//This code is contributed by Niraj_Pandey


Python 3
# Recursive Python 3 program to 
# find maximum
  
# function to return maximum element
# using recursion
def findMaxRec(A, n):
  
    # if n = 0 means whole array 
    # has been traversed
    if (n == 1):
        return A[0]
    return max(A[n - 1], findMaxRec(A, n - 1))
  
# Driver Code
if __name__ == "__main__":
      
    A = [1, 4, 45, 6, -50, 10, 2]
    n = len(A)
    print(findMaxRec(A, n))
  
# This code is contributed by ita_c


C#
// Recursive C# program to find maximum 
using System;
  
class GFG
{
// function to return maximum 
// element using recursion 
public static int findMaxRec(int []A, 
                             int n) 
{ 
// if size = 0 means whole array 
// has been traversed 
if(n == 1) 
    return A[0]; 
      
    return Math.Max(A[n - 1], 
                    findMaxRec(A, n - 1)); 
} 
  
// Driver code 
static public void Main ()
{
    int []A = {1, 4, 45, 6, -50, 10, 2}; 
    int n = A.Length; 
      
    // Function calling 
    Console.WriteLine(findMaxRec(A, n)); 
}
}
  
// This code is contributed by Sach_Code


PHP


输出:

-50

递归方法来查找数组中的Maximum元素

方法:

  1. 获取要为其找到最大值的数组
  2. 根据以下内容递归找到最大值:
    • 从末尾递归遍历数组
    • 基本情况:如果剩余数组的长度为1,则返回唯一存在的元素,即arr [0]
      if(n == 1)
         return arr[0];
      
    • 递归调用:如果不满足基本条件,则通过从末尾传递小一号的数组(即从arr [0]到arr [n-1])来调用函数。
    • 返回语句:在每个递归调用中(基本情况除外),返回当前数组的最后一个元素(即arr [n-1])和从上一个递归调用返回的元素的最大值。
      return max(arr[n-1], recursive_function(arr, n-1));
      
  3. 将递归函数返回的元素打印为最大元素

递归函数的伪代码:

If there is single element, return it.
Else return maximum of following.
    a) Last Element
    b) Value returned by recursive call
       fir n-1 elements.

下面是上述方法的实现:

C++

// Recursive C++ program to find maximum
#include 
using namespace std;
  
// function to return maximum element using recursion
int findMaxRec(int A[], int n)
{
    // if n = 0 means whole array has been traversed
    if (n == 1)
        return A[0];
    return max(A[n-1], findMaxRec(A, n-1));
}
  
// driver code to test above function
int main()
{
    int A[] = {1, 4, 45, 6, -50, 10, 2};
    int n = sizeof(A)/sizeof(A[0]);
    cout <<  findMaxRec(A, n);
    return 0;
}

Java

// Recursive Java program to find maximum
import java.util.*;
  
class GFG {
      
    // function to return maximum element using recursion
    public static int findMaxRec(int A[], int n)
    {
      // if size = 0 means whole array
      // has been traversed
      if(n == 1)
        return A[0];
          
        return Math.max(A[n-1], findMaxRec(A, n-1));
    }
  
    // Driver code
    public static void main(String args[])
    {
        int A[] = {1, 4, 45, 6, -50, 10, 2};
        int n = A.length;
          
        // Function calling
        System.out.println(findMaxRec(A, n));
    }
}
  
//This code is contributed by Niraj_Pandey

的Python 3

# Recursive Python 3 program to 
# find maximum
  
# function to return maximum element
# using recursion
def findMaxRec(A, n):
  
    # if n = 0 means whole array 
    # has been traversed
    if (n == 1):
        return A[0]
    return max(A[n - 1], findMaxRec(A, n - 1))
  
# Driver Code
if __name__ == "__main__":
      
    A = [1, 4, 45, 6, -50, 10, 2]
    n = len(A)
    print(findMaxRec(A, n))
  
# This code is contributed by ita_c

C#

// Recursive C# program to find maximum 
using System;
  
class GFG
{
// function to return maximum 
// element using recursion 
public static int findMaxRec(int []A, 
                             int n) 
{ 
// if size = 0 means whole array 
// has been traversed 
if(n == 1) 
    return A[0]; 
      
    return Math.Max(A[n - 1], 
                    findMaxRec(A, n - 1)); 
} 
  
// Driver code 
static public void Main ()
{
    int []A = {1, 4, 45, 6, -50, 10, 2}; 
    int n = A.Length; 
      
    // Function calling 
    Console.WriteLine(findMaxRec(A, n)); 
}
}
  
// This code is contributed by Sach_Code

的PHP


输出:

45

相关文章:
程序以查找数组中的最大元素