VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Sort numeric arrays in ascending (Asc) and decending (Dec) order using Bobble Sort, Selection Sort,

by Syed Shariq Rasheed (3 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Mon 26th February 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Sort numeric arrays in ascending (Asc) and decending (Dec) order using Bobble Sort, Selection Sort, and Shell Sort.

Rate Sort numeric arrays in ascending (Asc) and decending (Dec) order using Bobble Sort, Selection Sort,



'Author:    Syed Shariq Rasheed
'Author's email:    [email protected]
'Compatibility:    VB 6, VB 5, VB 4/32


*********************
'Task:    Sort numeric arrays in ascending (Asc) and decending (Dec) order using
'Bobble Sort, Selection Sort, and Shell Sort.
*********************

Private Sub Command1_Click()

'========================
'Variable Name                    Type
'========================
   Dim dblArray(9)          As Double
   Dim varTemp1            As Variant
   Dim varTemp2            As Variant
   Dim varTemp3            As Variant
   Dim varTemp4            As Variant
   Dim varTemp5            As Variant
   Dim varTemp6            As Variant
   Dim intLoop                As Integer

   '=========================================
   'Generate random numbers and assign the values to the array
   '=========================================
   Randomize
   For intLoop = LBound(dblArray) To UBound(dblArray)
      dblArray(intLoop) = Int(Rnd * 100) + 1
   Next intLoop

   '======================================
   'Assigning the array to the Temp variables of type variant
   '======================================
   varTemp1 = dblArray
   varTemp2 = dblArray
   varTemp3 = dblArray
   varTemp4 = dblArray
   varTemp5 = dblArray
   varTemp6 = dblArray

  '=================
  'Printing the orignal array
  '=================
   For intLoop = LBound(dblArray) To UBound(dblArray)
      Me.Print dblArray(intLoop); "";
   Next intLoop

   Call BubbleSortAsc(varTemp1)    'Calling BubbleSortAsc procedure to
   Me.Print                                       'sort the array in ascending order
   Me.Print                                       'and then print the sorted array
   For intLoop = LBound(varTemp1) To UBound(varTemp1)
      Me.Print varTemp1(intLoop); "";
   Next intLoop

   Call SelectionSortAsc(varTemp2)    'Calling SelectionSortAsc procedure to
   Me.Print                                         'sort the array in ascending order
   Me.Print                                         'and then print the sorted array
   For intLoop = LBound(varTemp2) To UBound(varTemp2)
      Me.Print varTemp2(intLoop); "";
   Next intLoop

   Call ShellSortAsc(varTemp3)       'Calling ShellSortAsc procedure to
   Me.Print                                       'sort the array in ascending order
   Me.Print                                       'and then print the sorted array
   For intLoop = LBound(varTemp3) To UBound(varTemp3)
      Me.Print varTemp3(intLoop); "";
   Next intLoop

  Call BubbleSortDec(varTemp4)    'Calling BubbleSortDec procedure to
   Me.Print                                      'sort the array in decending order
   Me.Print                                      'and then print the sorted array
   For intLoop = LBound(varTemp4) To UBound(varTemp4)
      Me.Print varTemp4(intLoop); "";
   Next intLoop

   Call SelectionSortDec(varTemp5)    'Calling SelectionSortDec procedure to
   Me.Print                                          'sort the array in decending order
   Me.Print                                          'and then print the sorted array
   For intLoop = LBound(varTemp5) To UBound(varTemp5)
      Me.Print varTemp5(intLoop); "";
   Next intLoop

   Call ShellSortDec(varTemp6)         'Calling ShellSortDec procedure to
   Me.Print                                         'sort the array in decending order
   Me.Print                                         'and then print the sorted array
   For intLoop = LBound(varTemp6) To UBound(varTemp6)
      Me.Print varTemp6(intLoop); "";
   Next intLoop

End Sub

******

Private Sub BubbleSortDec(varArray As Variant)

   Dim intLoop1 As Integer
   Dim intLoop2 As Integer
   Dim varTemp As Variant

   For intLoop1 = UBound(varArray) To LBound(varArray) Step -1
      For intLoop2 = LBound(varArray) + 1 To intLoop1
         If varArray(intLoop2) > varArray(intLoop2 - 1) Then
            varTemp = varArray(intLoop2)
            varArray(intLoop2) = varArray(intLoop2 - 1)
            varArray(intLoop2 - 1) = varTemp
         End If
      Next intLoop2
   Next intLoop1

End Sub

******

Private Sub BubbleSortAsc(varArray As Variant)

   Dim intLoop1 As Integer
   Dim intLoop2 As Integer
   Dim varTemp As Variant

   For intLoop1 = UBound(varArray) To LBound(varArray) Step -1
      For intLoop2 = LBound(varArray) + 1 To intLoop1
         If varArray(intLoop2 - 1) > varArray(intLoop2) Then
            varTemp = varArray(intLoop2 - 1)
            varArray(intLoop2 - 1) = varArray(intLoop2)
            varArray(intLoop2) = varTemp
         End If
      Next intLoop2
   Next intLoop1

End Sub

******

Private Sub SelectionSortAsc(varArray As Variant)

   Dim intLoop1 As Integer
   Dim intLoop2 As Integer
   Dim intMin As Integer
   Dim varTemp As Variant

   For intLoop1 = LBound(varArray) To UBound(varArray) - 1
      intMin = intLoop1
      For intLoop2 = intLoop1 + 1 To UBound(varArray)
         If varArray(intLoop2) < varArray(intMin) Then
            intMin = intLoop2
         End If
      Next intLoop2
      varTemp = varArray(intMin)
      varArray(intMin) = varArray(intLoop1)
      varArray(intLoop1) = varTemp
   Next intLoop1

End Sub

******

Private Sub SelectionSortDec(varArray As Variant)

   Dim intLoop1 As Integer
   Dim intLoop2 As Integer
   Dim intMin As Integer
   Dim varTemp As Variant

   For intLoop1 = LBound(varArray) To UBound(varArray) - 1
      intMin = intLoop1
      For intLoop2 = intLoop1 + 1 To UBound(varArray)
         If varArray(intMin) < varArray(intLoop2) Then
            intMin = intLoop2
         End If
      Next intLoop2
      varTemp = varArray(intMin)
      varArray(intMin) = varArray(intLoop1)
      varArray(intLoop1) = varTemp
   Next intLoop1

End Sub

******

Private Sub ShellSortAsc(varArray As Variant)

   Dim intLoop1 As Integer
   Dim intHold As Integer
   Dim intHValue As Integer
   Dim varTemp As Variant

   intHValue = LBound(varArray)
   Do
      intHValue = 3 * intHValue + 1
   Loop Until intHValue > UBound(varArray)
   Do
      intHValue = intHValue / 3
      For intLoop1 = intHValue + LBound(varArray) To UBound(varArray)
         varTemp = varArray(intLoop1)
         intHold = intLoop1
         Do While varArray(intHold - intHValue) > varTemp
            varArray(intHold) = varArray(intHold - intHValue)
            intHold = intHold - intHValue
            If intHold < intHValue Then Exit Do
         Loop
         varArray(intHold) = varTemp
      Next intLoop1
   Loop Until intHValue = LBound(varArray)

End Sub

******

Private Sub ShellSortDec(varArray As Variant)

   Dim intLoop1 As Integer
   Dim intHold As Integer
   Dim intHValue As Integer
   Dim varTemp As Variant

   intHValue = LBound(varArray)
   Do
      intHValue = 3 * intHValue + 1
   Loop Until intHValue > UBound(varArray)
   Do
      intHValue = intHValue / 3
      For intLoop1 = intHValue + LBound(varArray) To UBound(varArray)
         varTemp = varArray(intLoop1)
         intHold = intLoop1
         Do While varTemp > varArray(intHold - intHValue)
            varArray(intHold) = varArray(intHold - intHValue)
            intHold = intHold - intHValue
            If intHold < intHValue Then Exit Do
         Loop
         varArray(intHold) = varTemp
      Next intLoop1
   Loop Until intHValue = LBound(varArray)

End Sub

******


 

Download this snippet    Add to My Saved Code

Sort numeric arrays in ascending (Asc) and decending (Dec) order using Bobble Sort, Selection Sort, Comments

No comments have been posted about Sort numeric arrays in ascending (Asc) and decending (Dec) order using Bobble Sort, Selection Sort,. Why not be the first to post a comment about Sort numeric arrays in ascending (Asc) and decending (Dec) order using Bobble Sort, Selection Sort,.

Post your comment

Subject:
Message:
0/1000 characters