Heya Ich versuche, Auswahl Sortieralgorithmus auf einer einfach verknüpften Liste zu implementieren, ich bin mir bewusst, dass es ein Problem im Code ist, aber obwohl Meine verkettete Liste enthält die Nummern 7 1 2 6 Die Ausgabe nach dem Ausführen ist 7777. Jede Hilfe wäre willkommen.Implementieren Auswahl sortieren auf einer einfach verknüpften Liste
template<class Type>
void UnOrderedLinkedList<Type>::selectionSort()
{
nodeType<Type>* loc;
nodeType<Type>* minIndex;
nodeType<Type>* temp;
temp = first;
if(temp == NULL)
cerr<<"Cannot sort an empty list."<<endl;
else
if(temp->link == NULL)
cerr<<"List has only one item so it is already sorted."<<endl;
else
while(temp != NULL)
{
minIndex = minLocation(temp, last);
swap(temp, minIndex);
temp = temp->link;
}
}
template<class Type>
nodeType<Type>* UnOrderedLinkedList<Type>::minLocation(nodeType<Type>* first, nodeType<Type>* last)
nodeType<Type>* minIndex;
nodeType<Type>* other;
minIndex = first;
other = minIndex->link;
while(other != NULL)
{
if(minIndex->info > other->info)
{
minIndex = other;
other = other->link;
}
else
{
other = other->link;
}
}
return minIndex;
}
Dann zu tauschen:
template<class Type>
void UnOrderedLinkedList<Type>::swap(nodeType<Type>* first, nodeType<Type>* second)
{
nodeType<Type>* temp;
temp->info = first->info;
first->info = second->info;
second->info = temp->info;
}
Joachim Vielen Dank mein Code funktioniert jetzt perfekt! :) – Didem