# Thread: Recursive Method ==> find minimum value from array

1. Member Join Date
Feb 2008
Posts
3
Rep Power
0

## Recursive Method ==> find minimum value from array

Hello, this is my first post; sorry if my format is off, but I need to use a recursive method to find the minimum value of an array of integers. I feel like I'm almost there, but it is not working. I continuously get the value of zero returned. I may be referencing the position in the array, rather than getting the value, but I'm not sure.

I am aware that this can be done without using recursion, however, I need to do this recursively. If anyone can help, it would be greatly appreciated. Thanks in advance! My code is posted below:

Java Code:
```import java.io.*;
public class Main{
public static int minimum(int[] data, int first, int last) {
int M2;
int M1;
int mid;
int min;

if(first==last)
{
min= data;
return min;
}
else
{
mid = data[(first + last)/2];

M2= minimum(data, (++mid), last);
M1= minimum(data, (++first), mid);

if(M2<M1)
return M2;
else
return M1;}

}
public static void main(String[] args) {

int firstIndex, lastIndex;
int[] ints = new int;
ints = 20;
ints = 18;
ints = 40;
ints = 34;
ints = 50;
ints = 40;
ints = 20;
ints = 13;
ints = 98;
ints = 1;
int f = 4;
int val = 20;
firstIndex = 0;
lastIndex = ints.length - 1;

System.out.println("the minimum value from the ints array is " + minimum(ints, firstIndex, lastIndex));

}
}```  Reply With Quote

2. Member Join Date
Dec 2007
Posts
21
Rep Power
0

## Java Code:
```//import java.io.*;
public class temp1class{

public static int Min(int[] data, int curr, int currMin) {
if (curr == 0){
if (currMin > data[curr]){
return	data[curr];
} else return currMin;
} else {
if (currMin > data[curr])
currMin = data[curr];
curr--;
return Min(data, curr, currMin);

}

}
public static void main(String[] args) {

int[] ints = new int;
ints = 20;
ints = 18;
ints = 40;
ints = 34;
ints = 50;
ints = 40;
ints = 20;
ints = 20;
ints = 98;
ints = 1;

System.out.println("Min for currMin " + Min(ints, ints.length-1, ints[ints.length-1]));

}
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•