udfArrayShellSortM
arr udfArrayShellSortMV1 (arr)
arr udfArrayShellSortMV2 (arr)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayShellSortMV1 (arrArray)
If !ArrInfo (arrArray, -1) Then Return ArrDimension (0) ; Only dim-1 array allowed, 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
intMid = (intHigh - intLow + 1) / 2
While intMid
   intTop = intHigh - intMid
   For intI = intLow To intTop
      intK = intI + intMid
      If arrArray [intI] > arrArray [intK]
         anyValue = arrArray [intI]
         arrArray [intI] = arrArray [intK]
         arrArray [intK] = anyValue
      EndIf
   Next
   For intI = intTop To intLow By -1
      intK = intI + intMid
      If arrArray [intI] > arrArray [intK]
         anyValue = arrArray [intI]
         arrArray [intI] = arrArray [intK]
         arrArray [intK] = anyValue
      EndIf
   Next
   intMid = intMid / 2
EndWhile
Return arrArray
;..........................................................................................................................................
; This sorting algorithm is extremely efficient for sorting small and medium sized arrays.
;
; Adapted from a VBA routine in Woody's Office Watch, 1998, Vol. 3, No. 51,
; http://www.woodyswatch.com/office/
;
; "Diminishing increment sort" algorithm by Donald Lewis Shell resp. Marlene Metzner.
; First called Shell-Metzner in an article in Creative Computing in 1976, after Marlene Metzner.
; See also: Donald Lewis Shell, A High-Speed Sorting Procedure, CACM, 2(7):30-32, July 1959.
;
; This algorithm was improperly called the Shell-Metzner sort
; by John P. Grillo, A Comparison of Sorts, Creative Computing, 2:76-80, Nov/Dec 1976.
; On 3 April 2003 Marlene Metzner Norton wrote:
; "I had nothing to do with the sort, and my name should never have been attached to it."
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayShellSortMV2 (arrArray)
If !ArrInfo (arrArray, -1) Then Return ArrDimension (0) ; Only dim-1 array allowed, 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
intMid = (intHigh - intLow + 1) / 2
While intMid
   intTop = intHigh - intMid
   For intI = intLow To intTop
      intK = intI + intMid
      If arrArray [intI] > arrArray [intK] Then ArraySwapElements (arrArray, intI, 0, 0, 0, 0, intK, 0, 0, 0, 0)
   Next
   For intI = intTop To intLow By -1
      intK = intI + intMid
      If arrArray [intI] > arrArray [intK] Then ArraySwapElements (arrArray, intI, 0, 0, 0, 0, intK, 0, 0, 0, 0)
   Next
   intMid = intMid / 2
EndWhile
Return arrArray
;..........................................................................................................................................
; This sorting algorithm is extremely efficient for sorting small and medium sized arrays.
;
; Adapted from a VBA routine in Woody's Office Watch, 1998, Vol. 3, No. 51,
; http://www.woodyswatch.com/office/
;
; "Diminishing increment sort" algorithm by Donald Lewis Shell resp. Marlene Metzner.
; First called Shell-Metzner in an article in Creative Computing in 1976, after Marlene Metzner.
; See also: Donald Lewis Shell, A High-Speed Sorting Procedure, CACM, 2(7):30-32, July 1959.
;
; This algorithm was improperly called the Shell-Metzner sort
; by John P. Grillo, A Comparison of Sorts, Creative Computing, 2:76-80, Nov/Dec 1976.
; On 3 April 2003 Marlene Metzner Norton wrote:
; "I had nothing to do with the sort, and my name should never have been attached to it."
;..........................................................................................................................................
; This UDF "udfArrayShellSortMV2" 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: udfArrayShellSortM (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 = udfArrayShellSortMV1 (arrB)
arrC = udfArrayShellSortMV2 (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