linked list - QuickSort on a LinkedList in VBA -
i've unfortunately inherited vba code uses linkedlists in vba nothing sorted, , needs sorted.
linkedlist example: http://support.microsoft.com/kb/166394
i'm trying quicksort on items translating following code linkedlist: vba array sort function?
but i'm having hard time following logic of function determine how translate non-numbered system linked list.
could comment code explain happening, or possibly in translation?
first, need linked list object. use array example. let's take 5 nodes simplicity of example.
'declaration of array dim linkedlist(0 4) node
now, time fill array. variable head
head of our linkedlist
:
dim integer = 0 dim currentnode node set currentnode = head.pnext while not currentnode.pnext currentnode 'walk rest of list end linkedlist(i) = currentnode = + 1 set currentnode = currentnode.pnext 'current pointer next node wend
our linkedlist
filled, can use quicksort. launch initial call line :
quicksort linkedlist, lbound(linkedlist), ubound(linkedlist)
and adapt little function :
public sub quicksort(varray node, inlow long, inhi long) dim pivot integer dim tmpswap integer dim tmplow long dim tmphi long tmplow = inlow tmphi = inhi pivot = varray((inlow + inhi) \ 2).key while (tmplow <= tmphi) while (varray(tmplow).key < pivot , tmplow < inhi) tmplow = tmplow + 1 wend while (pivot < varray(tmphi).key , tmphi > inlow) tmphi = tmphi - 1 wend if (tmplow <= tmphi) tmpswap = varray(tmplow).key varray(tmplow).key = varray(tmphi).key varray(tmphi).key = tmpswap tmplow = tmplow + 1 tmphi = tmphi - 1 end if wend if (inlow < tmphi) quicksort varray, inlow, tmphi if (tmplow < inhi) quicksort varray, tmplow, inhi end sub
i think it's good. tell me if there problem or misunderstanding.
Comments
Post a Comment