Sorting algorithms/Comb sort: Difference between revisions

m
→‎{{header|FreeBASIC}}: minor change + second version with different gap calculation
m (→‎{{header|FreeBASIC}}: removed redundant code)
m (→‎{{header|FreeBASIC}}: minor change + second version with different gap calculation)
Line 729:
 
=={{header|FreeBASIC}}==
<lang FreeBASICFreebasic>' version 0521-0710-20152016
' compile with: fbc -s console
' for boundryboundary checks on array's compile with: fbc -s console -exx
 
Sub compsort(bs() As IntegerLong)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As IntegerLong lb = LBound(bs)
Dim As IntegerLong ub = UBound(bs)
Dim As IntegerLong gap = ub - lb
Dim As IntegerLong done, i
 
Do
gap = Int (gap / 1.3)
If gap < 1 Then gap = 1
done = 0
For i = lb To ub - gap
If bs(i) > bs(i + gap) Then
Swap bs(i), bs(i + gap)
done = 1
End If
Next
Loop Until ((gap = 1) And (done = 0))
 
End Sub
 
Sub comp11sort(bs() As Long)
' sort from lower bound to the higher bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(bs)
Dim As Long ub = UBound(bs)
Dim As Long gap = ub - lb
Dim As Long done, i
 
Do
gap = Int(gap / 1.24733)
If gap = 9 Or gap = 10 Then
gap = 11
ElseIf gap < 1 Then
gap = 1
End If
done = 0
For i = lb To ub - gap
Line 757 ⟶ 783:
' ------=< MAIN >=------
 
Dim As IntegerLong i, array(-7 To 87)
 
Dim As IntegerLong a = LBound(array), b = UBound(array) -1
 
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
 
b = b + 1
 
Print "normal comb sort"
Line 773 ⟶ 797:
For i = a To b : Print Using "####"; array(i); : Next : Print
compsort(array()) ' sort the array
Print " sorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
 
Print
Print "comb11 sort"
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
Print "unsorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
comp11sort(array()) ' sort the array
Print " sorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
Line 783 ⟶ 818:
{{out}}
<pre>normal comb sort
unsorted -16 35 -41 -63 -7 26 1 7 -34 13 4 0 -52 -2 5 60 02
sorted -7 -6 -5 -4 -3 -2 -1 0 0 1 2 3 4 5 6 7</pre>
 
comb11 sort
unsorted 4 -7 -1 1 2 3 -3 7 0 -2 6 -5 5 -6 -4
sorted -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
 
=={{header|Go}}==
457

edits