I am trying to write a selection sort, but I am getting a outofbounds error. How do I fix this?
public class SelectionSort{
public static void selectSort(int[] arr){
int least = 0;
for(int i = 1; i < arr.length - 1; i++){
for(int j = i + 1 ; i < arr.length-1; j++){
if(arr[j] < arr[least]){
int temp = arr[j];
arr[least] = arr[j];
arr[j] = temp;
least = j;
}
}
}
}
public static void printArray(int[] arr){
for(int i = 0 ; i < arr.length-1; i++){
System.out.print(arr[i] + ",");
}
}
public static void main(String[] args){
int[] array = {2,59,1,3,8,7,4};
selectSort(array);
System.out.println("\n\n\n\n =========================\n");
printArray(array);
}
}