for project school have decided solve problem coding heapsort have issue. ("vector" vector sort , "n" number of elements in "vector")
here code :
void fixheap(int position,int length) { int next=2*position; int temp; while (next<=length) { if (next<length && vector[next]<vector[next+1]) { next++; } if (vector[position]<vector[next]) { temp=vector[position]; vector[position]=vector[next]; vector[next]=temp; position=next; next=2*position; } else { return; } } } void heapsort() { int counter; int temp; for(counter=(n-1)/2;counter!=0;counter--) { fixheap(counter,n-1); } for(counter=n-1;counter>0;counter--) { temp=vector[counter]; vector[counter]=vector[0]; vector[0]=temp; fixheap(0,counter-1); } display(); } when i'm doing fixheap(0,n-1) i'm putting next 0 , position @ 0 i'm not doing heap right. me fixing it?
also there other mistakes spotted may have overlooked?
the children of i-th node are @ 2*i , 2*i+1. if want sort every item in array, 1st node @ index 0. need fix child node index calculations accordingly.
Comments
Post a Comment