by Chris (9 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 1st October 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
completes a shell sort
API Declarations
Dim compare As Single
Dim number(51) As Integer
'generates the numbers
Private Sub cmdStart_Click()
lbloriginal.Caption = ""
clear
generate
End Sub
'displays info
Private Sub cmdDisplay_Click()
clear
sort
display
End Sub
'clears labels and sets compare to zero
Public Sub clear()
compare = 0
mean = 0
total = 0
lblCompare.Caption = ""
lblcolumn1.Caption = ""
lblcolumn2.Caption = ""
lblcolumn3.Caption = ""
lblMean.Caption = ""
lblmedian.Caption = ""
End Sub
'generates the numbers
Public Sub generate()
Randomize
For x = 1 To 51
number(x) = Int((899 * Rnd) + 100) 'generates a random integer
lbloriginal.Caption = lbloriginal.Caption + Str(number(x))
Next
End Sub
' the shell sort routine
Public Sub sort()
v = 51
y = v
Do Until y = 0
y = Int(y / 2)
' If y = 0 Then
' Exit Do
' End If
k = v - y
g = 1
Do
f = g
Do
b = f + y
If number(f) <= number(b) Then
compare = compare + 1
Exit Do
End If
i = number(f)
number(f) = number(b)
number(b) = i
f = f - y
Loop Until f <= 0
' If f <= 0 Then
' Exit Do
' End If
g = g + 1
Loop Until g > k
Loop
End Sub
' displays in ascending and descending order
Public Sub display()
If optAsc.Value = True Then
For x = 1 To 17
lblcolumn1.Caption = lblcolumn1.Caption + Chr(13) + Str(number(x))
Next
For x = 18 To 34
lblcolumn2.Caption = lblcolumn2.Caption + Chr(13) + Str(number(x))
Next
For x = 35 To 51
lblcolumn3.Caption = lblcolumn3.Caption + Chr(13) + Str(number(x))
Next
ElseIf optDesc.Value = True Then
For x = 51 To 35 Step -1
lblcolumn1.Caption = lblcolumn1.Caption + Chr(13) + Str(number(x))
Next
For x = 34 To 18 Step -1
lblcolumn2.Caption = lblcolumn2.Caption + Chr(13) + Str(number(x))
Next
For x = 17 To 1 Step -1
lblcolumn3.Caption = lblcolumn3.Caption + Chr(13) + Str(number(x))
Next
End If
For x = 1 To 51
total = total + number(x)
mean = Int(total / 51)
Next
lblMean.Caption = mean
lblmedian.Caption = lblmedian.Caption + Str(number(26))
lblCompare.Caption = "There were" + Str(compare) + " comparisons made"
End Sub
'ends the program
Private Sub cmdEnd_Click()
End
End Sub