'_Jane Doe
'_Ch10Prob2
'_4/22/99
'_Program generates 100 random numbers between 100 and 1000 inclusive, sorts them
'_and searches for a number entered by the user

Option Explicit
Dim Numbers(100) As Integer		' Stores integers to be searched
Const MAX_ITEMS = 100			' Maximum number of items in array
  
    
Private Sub cmdClear_Click()

    lstRandomNumbers.Clear              ' Clear the listboxes
    lstSorted.Clear
    
    cmdGenerate.SetFocus                ' Set the focus on the Generate button
    
    lblDisplay.Visible = False          ' Make the display label invisible
    picStoplight.Visible = False        ' Make the picture of the stoplight invisible
    picTrophy.Visible = False           ' Make the picture of the stoplight invisible

End Sub


Private Sub cmdExit_Click()
    
    End     				          ' End the program

End Sub


Private Sub cmdGenerate_Click()
    
    Dim x As Integer                              ' Loop variable
        
    lstSorted.Clear                               ' Clear the listboxes
    lstRandomNumbers.Clear
    
    lblDisplay.Visible = False                    ' Make the display label invisible
    picStoplight.Visible = False                  ' Make the picture of the stoplight invisible
    picTrophy.Visible = False                     ' Make the picture of the trophy invisible
    
    For x = 1 To MAX_ITEMS
        Numbers(x) = Int(901 * Rnd) + 100         ' Generating random numbers between 100
                                                  '      and 1000 inclusive
        lstRandomNumbers.AddItem Str$(Numbers(x)) ' Display the random numbers in a listbox
    Next x

End Sub


Private Sub cmdSort_Click()
    
    Dim j As Integer                       ' Loop variable
    Dim Gap As Single                      ' Gap for comb sort
    Dim Temp As Single                     ' Used to swap numbers
    Dim Swapped As Integer                 ' Becomes true if a swap occurred
    Dim x As Integer                       ' Loop variable
    Const SHRINK = 1.3                     ' Gap is reduced by 1.3 each time loop iteration

    lstSorted.Clear                        ' Clear the listbox
    
    ' *************************** Comb Sort ****************************************

    Gap = 100		  		   ' Initial gap size
    
    Do                                     ' Beginning of Comb Sort
    
         Gap = Int(Gap / SHRINK)
    
 		  If Gap < 1 Then Gap = 1 
		  Swapped = False

		  For j = 1 To MAX_ITEM - Gap

			If Numbers(j) Numbers(j + Gap) Then
                		Temp = Numbers(j)          ' Swap routine
                		Numbers(j) = Numbers(j + Gap)
                		Numbers(j + Gap) = Temp
                		Swapped = True
            	  	End If
    
         	  Next j
    
    Loop Until Not Swapped And Gap = 1     ' Loop ends if no swaps have occurred
      
    For x = 1 To 100
        lstSorted.AddItem Str$(Numbers(x)) ' Display the sorted numbers in a listbox
    Next x

End Sub