;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfArrayQuickSortNRV1 (arrArray) ; Non recursive QuickSort. 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. intHigh = intElements - 1 intLow = 0 arrStackLeft = ArrDimension (100) arrStackRight = ArrDimension (100) arrStackLeft [1] = intLow arrStackRight [1] = intHigh intStack = 1 While intStack If arrStackLeft [intStack] >= arrStackRight [intStack] intStack = intStack - 1 Else intL = arrStackLeft [intStack] intR = arrStackRight [intStack] anyPivot = arrArray [intR] intMid = (intL + intR) / 2 If (intR - intL) > 5 If ((arrArray [intMid] < anyPivot) && (arrArray [intMid] > arrArray [intL])) || ((arrArray [intMid] > anyPivot) && (arrArray [intMid] < arrArray [intL])) anyValue = arrArray [intMid] arrArray [intMid] = arrArray [intR] arrArray [intR] = anyValue EndIf Else If ((arrArray [intL] < arrArray [intMid]) && (arrArray [intL] > anyPivot)) || ((arrArray [intL] > arrArray [intMid]) && (arrArray [intL] < anyPivot)) anyValue = arrArray [intL] arrArray [intL] = arrArray [intR] arrArray [intR] = anyValue EndIf EndIf anyPivot = arrArray [intR] While intL < intR While arrArray [intL] < anyPivot intL = intL + 1 EndWhile intR = intR - 1 While (intL < intR) && (anyPivot < arrArray [intR]) intR = intR - 1 EndWhile If intL < intR anyValue = arrArray [intL] arrArray [intL] = arrArray [intR] arrArray [intR] = anyValue EndIf EndWhile intR = arrStackRight [intStack] anyValue = arrArray [intL] arrArray [intL] = arrArray [intR] arrArray [intR] = anyValue If (intL - arrStackLeft [intStack]) >= (arrStackRight [intStack] - intL) arrStackLeft [intStack + 1] = arrStackLeft [intStack] arrStackRight [intStack + 1] = intL - 1 arrStackLeft [intStack] = intL + 1 Else arrStackLeft [intStack + 1] = intL + 1 arrStackRight [intStack + 1] = arrStackRight [intStack] arrStackRight [intStack] = intL - 1 EndIf intStack = intStack + 1 EndIf EndWhile Drop (arrStackLeft, arrStackRight) Return arrArray #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfArrayQuickSortNRV2 (arrArray) ; Non recursive QuickSort. 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. intHigh = intElements - 1 intLow = 0 arrStackLeft = ArrDimension (100) arrStackRight = ArrDimension (100) arrStackLeft [1] = intLow arrStackRight [1] = intHigh intStack = 1 While intStack If arrStackLeft [intStack] >= arrStackRight [intStack] intStack = intStack - 1 Else intL = arrStackLeft [intStack] intR = arrStackRight [intStack] anyPivot = arrArray [intR] intMid = (intL + intR) / 2 If (intR - intL) > 5 If ((arrArray [intMid] < anyPivot) && (arrArray [intMid] > arrArray [intL])) || ((arrArray [intMid] > anyPivot) && (arrArray [intMid] < arrArray [intL])) Then ArraySwapElements (arrArray, intMid, 0, 0, 0, 0, intR, 0, 0, 0, 0) Else If ((arrArray [intL] < arrArray [intMid]) && (arrArray [intL] > anyPivot)) || ((arrArray [intL] > arrArray [intMid]) && (arrArray [intL] < anyPivot)) Then ArraySwapElements (arrArray, intL, 0, 0, 0, 0, intR, 0, 0, 0, 0) EndIf anyPivot = arrArray [intR] While intL < intR While arrArray [intL] < anyPivot intL = intL + 1 EndWhile intR = intR - 1 While (intL < intR) && (anyPivot < arrArray [intR]) intR = intR - 1 EndWhile If intL < intR Then ArraySwapElements (arrArray, intL, 0, 0, 0, 0, intR, 0, 0, 0, 0) EndWhile intR = arrStackRight [intStack] ArraySwapElements (arrArray, intL, 0, 0, 0, 0, intR, 0, 0, 0, 0) If (intL - arrStackLeft [intStack]) >= (arrStackRight [intStack] - intL) arrStackLeft [intStack + 1] = arrStackLeft [intStack] arrStackLeft [intStack] = intL + 1 arrStackRight [intStack + 1] = intL - 1 Else arrStackRight [intStack + 1] = arrStackRight [intStack] arrStackRight [intStack] = intL - 1 arrStackLeft [intStack + 1] = intL + 1 EndIf intStack = intStack + 1 EndIf EndWhile Drop (arrStackLeft, arrStackRight) Return arrArray ;.......................................................................................................................................... ; This UDF "udfArrayQuickSortNRV2" needs minimal WinBatch version DLL 5.13bem, first showing up in WB 2007B. ;.......................................................................................................................................... #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: udfArrayQuickSortNR (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) arrC = Arrayize (strItemList, strDelimiter) arrB = udfArrayQuickSortNRV1 (arrB) arrC = udfArrayQuickSortNRV2 (arrC) 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" strMsgText = strMsgText : "Array C sorted: " : @LF : udfArrayItemize (arrC, strDelimiter) : @LF : @LF ; "/z,-196,1,22,999,a,B5,d,e,f,q,r,s,t,T,w" Message (strMsgTitle, strMsgText) Exit