Let's add more log, and you can see clearly:
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
public class Recursive {
private static AtomicInteger step = new AtomicInteger();
public static int minimum(int array[], int first, int last) {
int answer;
int mid;
int minFirst;
int minSecond;
int currentStep = step.incrementAndGet();
System.out.println("=================== step: " + currentStep + "=============");
int[] currentArray = new int[last - first];
System.arraycopy(array, first, currentArray, 0, currentArray.length);
System.out.println("current array(" + first + ", " + last + "): " + Arrays.toString(currentArray));
System.out.print(String.format("first(%d) == last(%d): %b", first, last, first == last));
if(first == last) {
System.out.println(", so min = " + array[first]);
return array[first];
}
else {
System.out.println(", so divide 2 the array");
mid = (first + last) / 2;
minFirst = minimum(array, first, mid);
int minFirstStep = step.get();
minSecond = minimum(array, mid + 1, last);
int minSecondStep = step.get();
System.out.print(String.format("compare min of step[%d] and step[%d]: %d < %d = %b",
minFirstStep,
minSecondStep,
minFirst,
minSecond,
minFirst < minSecond));
}
if(minFirst < minSecond)
answer = minFirst;
else
answer = minSecond;
System.out.println(", so min = " + answer);
return answer;
}
public static void main(String[] args) {
int array[] = {1, 4, 7, 8, 5, 2};
int min = minimum(array, 0, array.length - 1);
System.out.println("min is: " + min);
}
}
Output:
=================== step: 1=============
current array(0, 5): [1, 4, 7, 8, 5]
first(0) == last(5): false, so divide 2 the array
=================== step: 2=============
current array(0, 2): [1, 4]
first(0) == last(2): false, so divide 2 the array
=================== step: 3=============
current array(0, 1): [1]
first(0) == last(1): false, so divide 2 the array
=================== step: 4=============
current array(0, 0): []
first(0) == last(0): true, so min = 1
=================== step: 5=============
current array(1, 1): []
first(1) == last(1): true, so min = 4
compare min of step[4] and step[5]: 1 < 4 = true, so min = 1
=================== step: 6=============
current array(2, 2): []
first(2) == last(2): true, so min = 7
compare min of step[5] and step[6]: 1 < 7 = true, so min = 1
=================== step: 7=============
current array(3, 5): [8, 5]
first(3) == last(5): false, so divide 2 the array
=================== step: 8=============
current array(3, 4): [8]
first(3) == last(4): false, so divide 2 the array
=================== step: 9=============
current array(3, 3): []
first(3) == last(3): true, so min = 8
=================== step: 10=============
current array(4, 4): []
first(4) == last(4): true, so min = 5
compare min of step[9] and step[10]: 8 < 5 = false, so min = 5
=================== step: 11=============
current array(5, 5): []
first(5) == last(5): true, so min = 2
compare min of step[10] and step[11]: 5 < 2 = false, so min = 2
compare min of step[6] and step[11]: 1 < 2 = true, so min = 1
min is: 1