I'm trying to make a selection sort algorithm, just to train algorithms myself, to my see my logic is right about sorting, but it's returning me an error I do not understand. >
Code:
import java.util.Scanner;
import java.util.Arrays;
public class AlgoritmosOrdenacao {
static int[] SelectionSort(int vet[]){
int aux, menor;
for (int i=0; i<vet.length-2; i++){
menor = vet[i+1];
for (int j=i+1; j<vet.length-1; j++){
if (menor > vet[j+1]){
menor = vet[j+1];
}
}
aux = vet[i];
int idx = Arrays.asList(vet).indexOf(menor);
vet[i] = menor;
vet[idx] = aux;
}
return vet;
}
public static void main(String[] args) {
// TODO code application logic here
// INPUT DO VETOR
int vet[] = new int[5];
Scanner teclado = new Scanner(System.in);
for (int i=0; i<vet.length; i++){
vet[i] = teclado.nextInt();
}
// PRINT DO VETOR ANTES DA ORDENAÇÃO
for (int z=0; z<vet.length; z++){
System.out.printf("Vet[%d] = %d\n", z, vet[z]);
}
SelectionSort(vet);
// PRINT DO VETOR DEPOIS DA ORDENAÇÃO
System.out.println("Depois da ordenação");
for (int z=0; z<vet.length; z++){
System.out.printf("Vet[%d] = %d\n", z, vet[z]);
}
}
And the complete error is this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at algoritmosordenacao.AlgoritmosOrdenacao.SelectionSort(AlgoritmosOrdenacao.java:48)
at algoritmosordenacao.AlgoritmosOrdenacao.main(AlgoritmosOrdenacao.java:69)
C:\Users\angel\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 5 segundos)
In the line: int idx = Arrays.asList(vet).indexOf(menor);
Netbeans underlines vet
and gives the following suggestion:
confusing primitive array specified for the vararg method.