1
I am working on sorting algorithms. I have implemented sorting with Bubble Sort, however, must be implemented with an example where the algorithm is not stable( Selection ,Quicksort, Heapsort or Introsort). The idea is to sort a list of integers
Implementation with Bubble Sort
import java.util.LinkedList;
import java.util.List;
public class Ordenação {
public static void main(String[] args) {
List<Integer> lista = new LinkedList<Integer>();
lista.add(5);
lista.add(3);
lista.add(1);
lista.add(2);
lista.add(4);
ordenacaoBubleSort(lista);
}
public static void ordenacaoBubleSort(List<Integer> lista) {
for(int i = 0; i < lista.size(); i++) {
for(int j = 0; j < (lista.size() - 1 - i); j++) {
if(lista.get(j) > lista.get(j + 1)) {
Integer aux = lista.get(j);
lista.set(j, lista.get(j + 1));
lista.set(j + 1, aux);
}
System.out.println(lista);
}
}
System.out.println(lista);
}
//public static void ordenacaoSelecao(List<Integer>lista) {
//}
}
How could you adapt this Selection algorithm using a list ?
public void selectionSort(int vetor[]) {
for (int i = 0; i < vetor.length; i++) {
int posicaoMenor = i;
for (int j = (i + 1); j < vetor.length; j++) {
if (vetor[j] < vetor[posicaoMenor]) {
posicaoMenor = j;
}
}
if (vetor[i] != vetor[posicaoMenor]) {
int temp = vetor[i];
vetor[i] = vetor[posicaoMenor];
vetor[posicaoMenor] = temp;
}
}
}
And your doubt would be what?
– StatelessDev
I edited the code, my doubt would be how I could adapt the Code Selection of an integer to a list<integer>
– Felipe Machado