;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfArrayHeapSort (arrArray) If !ArrInfo (arrArray, -1) Then Return ArrDimension (0) ; Invalid input array, return empty valid dim-0 array. If ArrInfo (arrArray, 0) != 1 Then Return ArrDimension (0) ; Only dim-1 array allowed, return empty valid dim-0 array. intElements = ArrInfo (arrArray, 1) If intElements == 0 Then Return ArrDimension (0) ; Input array has no elements, return empty valid dim-0 array. If intElements == 1 Then Return arrArray ; Input array has only one element, return the input array. intR = intElements intM = (intElements / 2) - 1 For intI = intM To 0 By -1 intL = intI anyValue = arrArray [intL] While (intL < (intR / 2)) intNext = intL + intL + 1 If (intNext + 1) < intR Then If arrArray [intNext] < arrArray [intNext + 1] Then intNext = intNext + 1 If anyValue >= arrArray [intNext] Then Break arrArray [intL] = arrArray [intNext] intL = intNext EndWhile arrArray [intL] = anyValue Next While intElements > 1 anyValue = arrArray [0] arrArray [0] = arrArray [intElements - 1] arrArray [intElements - 1] = anyValue intElements = intElements - 1 intR = intElements intL = 0 anyValue = arrArray [intL] While intL < (intR / 2) intNext = intL + intL + 1 If (intNext + 1) < intR Then If arrArray [intNext] < arrArray [intNext + 1] Then intNext = intNext + 1 If anyValue >= arrArray [intNext] Then Break arrArray [intL] = arrArray [intNext] intL = intNext EndWhile arrArray [intL] = anyValue EndWhile Return arrArray ;.......................................................................................................................................... ; Heapsort algorithm adapted from: ; 'Algorithmen und Datenstrukturen, Prof. Dr. Gerald Timmer, 1998-11-06, Fachhochschule Osnabrück'. ; Reference: 'http://gtsun.et.fh-osnabrueck.de/lehre/algorithmen/alds-skript/node20.html'. ; ; Detlev Dalitz.20020822 ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ;---------------------------------------------------------------------------------------------------------------------- #DefineFunction udfArrayItemize (arrArray, strDelimiter) If !ArrInfo (arrArray, -1) Then Return "" ; No array. If !ArrInfo (arrArray, 6) Then Return "" ; No elements. If ArrInfo (arrArray, 0) > 1 Then Return "" ; Too much dimensions. strItemList = "" intHigh = Max (ArrInfo (arrArray, 1) - 1, 0) intLow = 0 For intElem = intLow To intHigh If !!VarType (arrArray [intElem]) strItemList = ItemInsert (arrArray [intElem], -1, strItemList, strDelimiter) Else strItemList = ItemInsert ("", -1, strItemList, strDelimiter) EndIf Next Return strItemList ;---------------------------------------------------------------------------------------------------------------------- ; This UDF "udfArrayItemize" converts a given dim-1 array into an itemlist ; with each item separated by delimiter character. ; ; Example: strMyItemList = udfArrayItemize (arrMyArray, @TAB) ; Creates an itemList from array. ; ; Note: ; This UDF supports only dim-1 array. ; An array element which is not initialized has a Vartype=0 (undefined). ; Therefore an empty item will be appended to target itemlist. ; ; Detlev Dalitz.20020718.20090508. ;---------------------------------------------------------------------------------------------------------------------- #EndFunction ;---------------------------------------------------------------------------------------------------------------------- ; Test. strMsgTitle = "Demo: udfArrayHeapSort (arrArray)" strMsgText = "" strDelimiter = "," strItemList = "22,q,w,e,r,t,T,999,a,s,d,f,1,/z,B5,-196" ; 16 Items. arrA = Arrayize (strItemList, strDelimiter) arrB = Arrayize (strItemList, strDelimiter) arrB = udfArrayHeapSort (arrB) strMsgText = strMsgText : "Array A: " : @LF : udfArrayItemize (arrA, strDelimiter) : @LF : @LF ; "22,q,w,e,r,t,T,999,a,s,d,f,1,/z,B5,-196" strMsgText = strMsgText : "Array B sorted: " : @LF : udfArrayItemize (arrB, strDelimiter) : @LF : @LF ; "/z,-196,1,22,999,a,B5,d,e,f,q,r,s,t,T,w" Message (strMsgTitle, strMsgText) Exit