Sort an integer Linkedlist without Sort method?

Asked

Viewed 382 times

2

I want to know a simple method to order a Linkedlist! I’m breaking my head and searching the Internet, but I just find methods that already perform the work that I WOULD LIKE manually perform.

   public class OrdenacaoTeste {

    public static void ordenaLista(LinkedList<Integer> lista) {

    }

    public static void main(String[] args) {
        LinkedList<Integer> lista = new LinkedList<Integer>();

        lista.add(1);
        lista.add(5);
        lista.add(3);
        lista.add(6);
        lista.add(8);


        System.out.println("lista não ordenada: " + lista);

        Collections.sort(lista); //ordena automaticamente
        System.out.println(lista);

        for (int i = 0; i < lista.size(); i++) {
            for (int j = 0; j < lista.size() - 1; j++) {

         }
      }



    }
}

My class gets the values, 1, 5, 3, 6, 8! I know how a linked list works, so I’m using the linkedlist to save work! But how can I as a loop for and perhaps conditions IFS order the list in ascending order?

I know that for ordering I will have to make ifs to compare the bigger and smaller, but wanted help on how to pass these values and how to work with them in the same class.

Thanks in advance.

  • http://stackoverflow.com/a/19263295/5524514

  • I do not see how this mode will help me to carry out the task manually?!

  • Your question gives to understand what you want to do manually. If that’s not what you want, I suggest you edit it and make what you want to make clear.

  • I believe the question is very clear, sorry for any inconvenience, I just want to order the values of this list in an increasing and manual way. For example, if it were a 5 position vector, I would do two for’s and one if(vet[j] > vet[j + 1]){ aux = vet[j]; vet[j] = vet[j+1]; vet[j+1] = aux; }.

  • I understand, I think this one will suit you right http://stackoverflow.com/a/6369923/5524514

  • Thanks @diegofm for their patience and comments!

Show 1 more comment

1 answer

1

I got you guys, I think the code is simple and self sufficient to explain

I also leave my many thanks to @diegofm, you are fuc4!

public class OrdenacaoTeste {
public static void ordenaLista(LinkedList<Integer> lista) {

}

public static void main(String[] args) {
    LinkedList<Integer> lista = new LinkedList<Integer>();

    lista.add(8);
    lista.add(9);
    lista.add(3);
    lista.add(5);
    lista.add(1);

    System.out.println("lista não ordenada: " + lista);

    for (int i = 0; i < lista.size() - 1; i++) { // percorro
        for (int j = 0; j < lista.size() - 1; j++) {
            if (lista.get(j) > lista.get(j + 1)) { // vejo se é maior que a
                                                    // prox posiçao
                Integer maior = lista.get(j);
                Integer menor = lista.get(j + 1);

                lista.remove(maior);
                lista.remove(menor);
                lista.add(j, menor);

                lista.add(j + 1, maior);
                // lista.remove(i);
            }
        }
        // Utilizando SORT
        // Collections.sort(lista);
        //System.out.println("Utilizando método sort" + lista);

        System.out.println("Lista ordenada em ordem crescente: " + lista); //imprimindo dentro do for
    }
    // System.out.println("Lista ordenada em ordem crescente" + lista); 
}

}

  • Ah, you wanted a style ordination Bubble Sort? I had understood that it was to implement your Sort manually instead of using the Collections, but I’m glad you were able to solve it =)

Browser other questions tagged

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