Skip to main content

Find minimum element in array using recursion

Here given code implementation process.

// C program
// Find minimum element in array using recursion
#include <stdio.h>

//Find min element recursively in given collection
int min_element(int collection[], int low, int high)
{
	if (low == high)
	{
		//When only one element then returning current element
		return collection[high];
	}
	// Find mid element
	// low is first element location
	// high is last element location
	int mid = (low + high) >> 1;
	//Find min element in left side
	int a = min_element(collection, low, mid);
	//Find min element in right side
	int b = min_element(collection, mid + 1, high);
	if (a < b)
	{
		// When a is min
		return a;
	}
	else
	{
		// When b is min
		return b;
	}
}
//Display element of given collection
void display_element(int collection[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", collection[i]);
	}
	printf("\n");
}
int main()
{
	// Define the unsorted array elements
	int collection[] = {
		7,
		3,
		8,
		23,
		3,
		2,
		9,
		35,
		13,
		42,
		1,
		3
	};
	//Get the size of given collection
	int size = sizeof(collection) / sizeof(collection[0]);
	printf("Element : ");
	display_element(collection, size);
	int result = min_element(collection, 0, size - 1);
	printf("Minimum Element : %d \n", result);
	return 0;
}

Output

Element :   7  3  8  23  3  2  9  35  13  42  1  3
Minimum Element : 1
/*
    Java Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	//Find min element recursively in given collection
	public int min_element(int[] collection, int low, int high)
	{
		if (low == high)
		{
			//When only one element then returning current element
			return collection[high];
		}
		// Find mid element
		// low is first element location
		// high is last element location
		int mid = (low + high) >> 1;
		//Find min element in left side
		int a = min_element(collection, low, mid);
		//Find min element in right side
		int b = min_element(collection, mid + 1, high);
		if (a < b)
		{
			// When a is min
			return a;
		}
		else
		{
			// When b is min
			return b;
		}
	}
	//Display element of given collection
	public void display_element(int[] collection, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + collection[i]);
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyRecursion obj = new MyRecursion();
		// Define the unsorted array elements
		int[] collection = {
			7,
			3,
			8,
			23,
			3,
			2,
			9,
			35,
			13,
			42,
			1,
			3
		};
		//Get the size of given collection
		int size = collection.length;
		System.out.print("Element : ");
		obj.display_element(collection, size);
		int result = obj.min_element(collection, 0, size - 1);
		System.out.print("Minimum Element : " + result + " \n");
	}
}

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1
//Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	public:
		//Find min element recursively in given collection
		int min_element(int collection[], int low, int high)
		{
			if (low == high)
			{
				//When only one element then returning current element
				return collection[high];
			}
			// Find mid element
			// low is first element location
			// high is last element location
			int mid = (low + high) >> 1;
			//Find min element in left side
			int a = this->min_element(collection, low, mid);
			//Find min element in right side
			int b = this->min_element(collection, mid + 1, high);
			if (a < b)
			{
				// When a is min
				return a;
			}
			else
			{
				// When b is min
				return b;
			}
		}
	//Display element of given collection
	void display_element(int collection[], int size)
	{
		for (int i = 0; i < size; ++i)
		{
			cout << " " << collection[i];
		}
		cout << "\n";
	}
};
int main()
{
	MyRecursion obj = MyRecursion();
	// Define the unsorted array elements
	int collection[] = {
		7 , 3 , 8 , 23 , 3 , 2 , 9 , 35 , 13 , 42 , 1 , 3
	};
	//Get the size of given collection
	int size = sizeof(collection) / sizeof(collection[0]);
	cout << "Element : ";
	obj.display_element(collection, size);
	int result = obj.min_element(collection, 0, size - 1);
	cout << "Minimum Element : " << result << " \n";
	return 0;
}

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1
//Include namespace system
using System;
/*
    C# Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	//Find min element recursively in given collection
	public int min_element(int[] collection, int low, int high)
	{
		if (low == high)
		{
			//When only one element then returning current element
			return collection[high];
		}
		// Find mid element
		// low is first element location
		// high is last element location
		int mid = (low + high) >> 1;
		//Find min element in left side
		int a = min_element(collection, low, mid);
		//Find min element in right side
		int b = min_element(collection, mid + 1, high);
		if (a < b)
		{
			// When a is min
			return a;
		}
		else
		{
			// When b is min
			return b;
		}
	}
	//Display element of given collection
	public void display_element(int[] collection, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + collection[i]);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyRecursion obj = new MyRecursion();
		// Define the unsorted array elements
		int[] collection = {
			7 , 3 , 8 , 23 , 3 , 2 , 9 , 35 , 13 , 42 , 1 , 3
		};
		//Get the size of given collection
		int size = collection.Length;
		Console.Write("Element : ");
		obj.display_element(collection, size);
		int result = obj.min_element(collection, 0, size - 1);
		Console.Write("Minimum Element : " + result + " \n");
	}
}

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1
<?php
/*
    Php Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	//Find min element recursively in given collection
	public	function min_element( & $collection, $low, $high)
	{
		if ($low == $high)
		{
			//When only one element then returning current element
			return $collection[$high];
		}
		// Find mid element
		// low is first element location
		// high is last element location
		$mid = ($low + $high) >> 1;
		//Find min element in left side
		$a = $this->min_element($collection, $low, $mid);
		//Find min element in right side
		$b = $this->min_element($collection, $mid + 1, $high);
		if ($a < $b)
		{
			// When a is min
			return $a;
		}
		else
		{
			// When b is min
			return $b;
		}
	}
	//Display element of given collection
	public	function display_element( & $collection, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $collection[$i];
		}
		echo "\n";
	}
}

function main()
{
	$obj = new MyRecursion();
	// Define the unsorted array elements
	$collection = array(7, 3, 8, 23, 3, 2, 9, 35, 13, 42, 1, 3);
	//Get the size of given collection
	$size = count($collection);
	echo "Element : ";
	$obj->display_element($collection, $size);
	$result = $obj->min_element($collection, 0, $size - 1);
	echo "Minimum Element : ". $result ." \n";
}
main();

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1
/*
    Node Js Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	//Find min element recursively in given collection
	min_element(collection, low, high)
	{
		if (low == high)
		{
			//When only one element then returning current element
			return collection[high];
		}
		// Find mid element
		// low is first element location
		// high is last element location
		var mid = (low + high) >> 1;
		//Find min element in left side
		var a = this.min_element(collection, low, mid);
		//Find min element in right side
		var b = this.min_element(collection, mid + 1, high);
		if (a < b)
		{
			// When a is min
			return a;
		}
		else
		{
			// When b is min
			return b;
		}
	}
	//Display element of given collection
	display_element(collection, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + collection[i]);
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var obj = new MyRecursion();
	// Define the unsorted array elements
	var collection = [7, 3, 8, 23, 3, 2, 9, 35, 13, 42, 1, 3];
	//Get the size of given collection
	var size = collection.length;
	process.stdout.write("Element : ");
	obj.display_element(collection, size);
	var result = obj.min_element(collection, 0, size - 1);
	process.stdout.write("Minimum Element : " + result + " \n");
}
main();

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1
#  Python 3 Program
#  Find minimum element in array using recursion

class MyRecursion :
	# Find min element recursively in given collection
	def min_element(self, collection, low, high) :
		if (low == high) :
			# When only one element then returning current element
			return collection[high]
		
		#  Find mid element
		#  low is first element location
		#  high is last element location
		mid = (low + high) >> 1
		# Find min element in left side
		a = self.min_element(collection, low, mid)
		# Find min element in right side
		b = self.min_element(collection, mid + 1, high)
		if (a < b) :
			#  When a is min
			return a
		else :
			#  When b is min
			return b
		
	
	# Display element of given collection
	def display_element(self, collection, size) :
		i = 0
		while (i < size) :
			print(" ", collection[i], end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MyRecursion()
	#  Define the unsorted array elements
	collection = [7, 3, 8, 23, 3, 2, 9, 35, 13, 42, 1, 3]
	# Get the size of given collection
	size = len(collection)
	print("Element : ", end = "")
	obj.display_element(collection, size)
	result = obj.min_element(collection, 0, size - 1)
	print("Minimum Element : ", result ," \n", end = "")

if __name__ == "__main__": main()

Output

Element :   7  3  8  23  3  2  9  35  13  42  1  3
Minimum Element :  1
#  Ruby Program
#  Find minimum element in array using recursion

class MyRecursion

	# Find min element recursively in given collection
	def min_element(collection, low, high)
	
		if (low == high)
		
			# When only one element then returning current element
			return collection[high]
		end
		#  Find mid element
		#  low is first element location
		#  high is last element location
		mid = (low + high) >> 1
		# Find min element in left side
		a = self.min_element(collection, low, mid)
		# Find min element in right side
		b = self.min_element(collection, mid + 1, high)
		if (a < b)
		
			#  When a is min
			return a
		else
		
			#  When b is min
			return b
		end
	end
	# Display element of given collection
	def display_element(collection, size)
	
		i = 0
		while (i < size)
		
			print(" ", collection[i])
			i += 1
		end
		print("\n")
	end
end
def main()

	obj = MyRecursion.new()
	#  Define the unsorted array elements
	collection = [7, 3, 8, 23, 3, 2, 9, 35, 13, 42, 1, 3]
	# Get the size of given collection
	size = collection.length
	print("Element : ")
	obj.display_element(collection, size)
	result = obj.min_element(collection, 0, size - 1)
	print("Minimum Element : ", result ," \n")
end
main()

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1 
/*
    Scala Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	//Find min element recursively in given collection
	def min_element(collection: Array[Int], low: Int, high: Int): Int = {
		if (low == high)
		{
			//When only one element then returning current element
			return collection(high);
		}
		// Find mid element
		// low is first element location
		// high is last element location
		var mid: Int = (low + high) >> 1;
		//Find min element in left side
		var a: Int = min_element(collection, low, mid);
		//Find min element in right side
		var b: Int = min_element(collection, mid + 1, high);
		if (a < b)
		{
			// When a is min
			return a;
		}
		else
		{
			// When b is min
			return b;
		}
	}
	//Display element of given collection
	def display_element(collection: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + collection(i));
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyRecursion = new MyRecursion();
		// Define the unsorted array elements
		var collection: Array[Int] = Array(7, 3, 8, 23, 3, 2, 9, 35, 13, 42, 1, 3);
		//Get the size of given collection
		var size: Int = collection.length;
		print("Element : ");
		obj.display_element(collection, size);
		var result: Int = obj.min_element(collection, 0, size - 1);
		print("Minimum Element : " + result + " \n");
	}
}

Output

Element :  7 3 8 23 3 2 9 35 13 42 1 3
Minimum Element : 1
/*
    Swift Program
    Find minimum element in array using recursion
*/
class MyRecursion
{
	//Find min element recursively in given collection
	func min_element(_ collection: [Int], _ low: Int, _ high: Int) -> Int
	{
		if (low == high)
		{
			//When only one element then returning current element
			return collection[high];
		}
		// Find mid element
		// low is first element location
		// high is last element location
		let mid: Int = (low + high) >> 1;
		//Find min element in left side
		let a: Int = self.min_element(collection, low, mid);
		//Find min element in right side
		let b: Int = self.min_element(collection, mid + 1, high);
		if (a < b)
		{
			// When a is min
			return a;
		}
		else
		{
			// When b is min
			return b;
		}
	}
	//Display element of given collection
	func display_element(_ collection: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", collection[i], terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: MyRecursion = MyRecursion();
	// Define the unsorted array elements
	let collection: [Int] = [7, 3, 8, 23, 3, 2, 9, 35, 13, 42, 1, 3];
	//Get the size of given collection
	let size: Int = collection.count;
	print("Element : ", terminator: "");
	obj.display_element(collection, size);
	let result: Int = obj.min_element(collection, 0, size - 1);
	print("Minimum Element : ", result ," \n", terminator: "");
}
main();

Output

Element :   7  3  8  23  3  2  9  35  13  42  1  3
Minimum Element :  1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment