Jump to content
AJBM

[Resolvido] Duvida Insertion Sort

Recommended Posts

AJBM

Boas!

Eu estou aplicar o algoritmo Insertion Sort em listas ligadas e esta a funcionar, mas surgiu me uma duvida.

 public static boolean InsertionSort(ListaLigada lista) {

	int j, i;
	No percorrer = lista.getHead().getNext();

	for (j = 1; j < lista.getNelementos() - 1; j++) {
		int valor=percorrer.getElement();
		i = j - 1;
		No tmp;

		while ((i >= 0) && percorrer.getElement() < (tmp = lista.getNoByPosition(i)).getElement()) {

			tmp.getNext().setElement(tmp.getElement());


			// tmp.setElement(percorrer.getElement());// se usar isto nao funciona
			//o valor de pecorrer e alterado nao percebo porque
			// eu ja fiz debug e ele e alterado por causa disto tmp.getNext().setElement(tmp.getElement());

			tmp.setElement(valor);


			i--;
		}

		percorrer = percorrer.getNext();
	}



	return true;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.