I've only had BlitzPlus for a couple of weeks and I thought a sort function might be good practice and handy. Accordingly I looked up sorts on the net and wrote this Shell sort of a simple array. Shell is simple and you always know it has worked because the last pass has to be clean.
Anyway, most people seem to decrease the partition length by a factor of 2 or near enough - some sites advise using the sequence 2^n-1 or similar. In an idle moment I tried dividing the partition length each pass by real numbers instead of integers. It seems that dividing it by something near to 1.6 results in a sort about 75% faster than with powers of 2 on random data.
I'm uncertain whether or not I'm kidding myself about this. Surely this cannot be right or somebody else would have mentioned it. Have I discovered something or have I not ?
Most people seem to use 2 or some sequence based on powers of 2 where 1.6 occurs below.
Dim a(1000000)
SeedRnd MilliSecs()
For i=0 To 1000000
a(i)=Rnd(0,1000000)
Next
s=MilliSecs()
n=1000000
shellsort(n)
t=MilliSecs()
Print Str(t-s)
Function shellsort(n)
i=n
Repeat
i=Int(Float(i)/1.6)
Print Str(i)
lowest=0
For lowest=0 To i-1
Repeat
j=lowest
flag=0
While j+i<=n
If a(j)>a(j+i) Then
swap=a(j)
a(j)=a(j+i)
a(j+i)=swap
flag=1
EndIf
j=j+i
Wend
Until flag=0
Next
Until i<=1
End Function
Anyway, most people seem to decrease the partition length by a factor of 2 or near enough - some sites advise using the sequence 2^n-1 or similar. In an idle moment I tried dividing the partition length each pass by real numbers instead of integers. It seems that dividing it by something near to 1.6 results in a sort about 75% faster than with powers of 2 on random data.
I'm uncertain whether or not I'm kidding myself about this. Surely this cannot be right or somebody else would have mentioned it. Have I discovered something or have I not ?
Most people seem to use 2 or some sequence based on powers of 2 where 1.6 occurs below.
Dim a(1000000)
SeedRnd MilliSecs()
For i=0 To 1000000
a(i)=Rnd(0,1000000)
Next
s=MilliSecs()
n=1000000
shellsort(n)
t=MilliSecs()
Print Str(t-s)
Function shellsort(n)
i=n
Repeat
i=Int(Float(i)/1.6)
Print Str(i)
lowest=0
For lowest=0 To i-1
Repeat
j=lowest
flag=0
While j+i<=n
If a(j)>a(j+i) Then
swap=a(j)
a(j)=a(j+i)
a(j+i)=swap
flag=1
EndIf
j=j+i
Wend
Until flag=0
Next
Until i<=1
End Function