### selection sort for linked list

I'm trying to sort a linked list by using selection sort algorithm,but it doesn't work.where's the problem?! I test it by myself and it worked.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#include using namespace std; #include struct node { int value; struct node *link; }; void sort(node**); int main() { int tmp; node *start,*p,*q; cin>>tmp; p=new node; p->value=tmp; start=p; q=p; while(cin>>tmp) { p=new node; p->value=tmp; q->link=p; q=q->link; } q->link=NULL; sort(&start); p=start; while(p) { cout<< p->value<<" "; p=p->link; } cout<link) { tmp=p->value; while(p) { if(p->value<=tmp) { tmp=p->value; ind=i; } i++; p=p->link; } p=*start; for(int j=0;j<--ind;j++) p=p->link; if(p==*start) ; else { q=*start; *start=p->link; p->link=q; q=(p->link)->link; (p->link)->link=(*start)->link; (*start)->link=q; } sort(&((*start)->link)); } }``````
Last edited on
 ``12`` `````` for(int j=0;j<--ind;j++) p=p->link;``````

If ind is the "index" of the node with the smallest value, and you're trying to get to that node, why are you decreasing the value of ind while increasing the value of j?
WOW! thank you so much! I haven't noticed that it happens every time.
Topic archived. No new replies allowed.