Selection Sort - Is that correct?

Asked

Viewed 183 times

2

I implemented a logic of Selection Sort and would like to ask if it is correct. Because at the end of the process the result is expected, but went to make a real proof and found several algorithms but none similar to mine.

Follows the code.

int vetor[] = {9, 1, 3, 2, 5, 4, 7, 8, 6, 0};
int aux;

for(int i = 0; n = vetor.length; i < n; i++){
    for(int j = 0; j < n - 1; j++){
        if(vetor[j] > vetor[i]){
            aux = vetor[i];
            vetor[i] = vetor[j];
            vetor[j] = aux;
        }
    }
}// fim do for

// aqui mando imprimir o vetor ordenado
for(int i = 0, n = vetor.length; i < n; i++){
    System.out.println("" + vetor[i]);
}

1 answer

1


His logic is Bubble Sort, because he is always working with the next vector position. Selection Sort searches all vector positions for the lowest value and replaces the ordered index.

Selection Sort:

inserir a descrição da imagem aqui

Note that it always looks for the lowest value in the whole array, rather than changing the next one if the value is already lower.

Example of implementation (same place where I took the image) the two do the same thing but in different ways

  • Okay, thank you very much :)

  • Thank you so much for the help, this link gave to better understand perfect =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.