Giudoku Official Site
Finding the minimum element

This is not a "famous" algorithm, just an exercise I found on a book.

First, I need that overloading part just for the sake of precision. The recursion call, as you may note, needs a starting index - which would seem pointless to an extern user. The alternative is to pass as a parameter to each call a subarray extracted from the original minus the first element, and then slightly modify the base case; but that would have added useless complexity to the algorithm, so I chose the option you're going to see.

This is a particular neat use of recursion I particularly liked, because it remembers me of a dynamic programming algorithm I once learnt about, only one-dimensional. The subsequent calls take us to the last element of the array; at that point, the base case interrupts the game by returning the element itself.
Then the subsequent returns take us back to the first element, bringing with him the minimum element step by step.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Arrays;

public class RecursiveMinimum
{
	public static void main(String[] args)
	{
		int[] v1 = {1, 2, 3, 4, 5, 6};
		int[] v2 = {5, 21, 4, 7, 2, 8};
		
		System.out.println(Arrays.toString(v1) + " Minimo: " + recursiveMinimum(v1) + " [corretto: 1]");
		System.out.println(Arrays.toString(v2) + " Minimo: " + recursiveMinimum(v2) + " [corretto: 2]");
		
	}
	
	/*
	 * So the user is not forced to enter the starting element
	 * if it's obvious
	 */
	public static int recursiveMinimum(int[] V)
	{
		return recursiveMinimum(V, 0);
	}
	
	/*
	 * Recursively returns the minimum element in the array
	 */
	public static int recursiveMinimum(int[] V, int start)
	{
		if (start == V.length - 1)
		{
			return V[start];
		}
		
		return Minimum(V[start], recursiveMinimum(V, start+1));
	}
	
	/*
	 * Returns the minimum between two integers
	 */
	public static int Minimum(int n1, int n2)
	{
		if (n1 < n2)
		{
			return n1;
		}
		
		return n2;
	}
	
}