import java.util.Arrays;
public class RecSelect {
public static void Select(int[] ar, int idx, int min, int start) {
if(idx == ar.length) {
return;
}
if(idx < ar.length - start) {
if(ar[min] > ar[idx]) {
min = idx;
Select(ar, idx+1, min,0);
}
else {
Select(ar, idx+1, min,0);
}
int temp = ar[min];
ar[min] = ar[start];
ar[start] = temp;
Select(ar, start + 2, start + 1, start + 1);
}
}
public static void main(String[] args) {
int[] ar = {5, 3, 1, 8, 2};
Select(ar, 1, 0, 0);
System.out.println(Arrays.toString(ar));
}
}
Please guide me, why is this giving stack overflow error, although I have used a base condition.
Tell me what must be corrected.
There are another methods using for loop and one with creating a findMin() function.
But I want to do it this way. HELP!