Você está na página 1de 1

C:\Documents and Settings\oosteep\My Documents\data\Computer Science\Algorithms\O-Sort\O-sort_v4.

vbs Thursday, February 17, 2011 2:29 PM

1 ' ***********************************************************************
2 ' function sort - input an unsorted array and get a sorted array back
3 ' O-Sort (Oosterwal Sort) Version 3
4 ' ***********************************************************************
5 function sort(byref inputarray, byval direction) ' input array is array to be sorted,
direction is -1 for descending (defaults to ascending)
6
7 dim sngPhi, sngFib, temp, i, length, StepSize
8
9 length = ubound(inputarray)
10 SngPhi = 0.7245 ' Define phi value
11 temp = int(3.1027*log((length)*0.8678))
12 StepSize = int(1.1523*exp(0.3223*temp)+.5)-1
13
14 While ((StepSize > 0) or not isnull(temp))
15
16 temp = null
17 if not stepsize then stepsize = 1
18
19 select case direction
20 case -1 ' -1 means sort
the array in a descending fashion
21 For i = 0 To Length - StepSize ' Range of
lower cell
22 If inputArray(i) < inputArray(i + StepSize) Then ' Compare cells
23 temp = inputArray(i) ' \
24 inputArray(i) = inputArray(i + StepSize) ' | Swap Cells
25 inputArray(i + StepSize) = temp ' /
26 End If
27 Next
28
29 case else
30 For i = 0 To Length - StepSize ' Range of
lower cell
31 If InputArray(i) > inputArray(i + StepSize) Then ' Compare cells
32 temp = inputArray(i) ' \
33 inputArray(i) = inputArray(i + StepSize) ' | Swap Cells
34 inputArray(i + StepSize) = temp ' /
35 End If
36 Next
37 end select
38
39 StepSize = Int(stepsize*Sngphi) ' Set the integer step value
40
41 Wend
42
43 sort = inputarray
44
45 end function
46

-1-

Você também pode gostar