Selection Sort - Is it correct?

2

I have implemented a sort sort logic and would like to ask if it is correct. Because at the end of the process the result is expected, but was to do a real test and found several algorithms but none like my.

Follow 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]);
}
    
asked by anonymous 12.07.2017 / 19:08

1 answer

1

Its logic is Bubble Sort, because it is always working with the next position of the vector. Selection Sort searches all positions of the vector for the lowest value and makes the substitution in the ordered index.

Selection Sort:

Notethatitalwayslooksforthelowestvalueinanyvector,insteadofchangingthenextcasethevalueisalreadylower.

Implementation example (same place where I removed the image) the two do the same thing but in different ways

    
12.07.2017 / 19:18