Advertisement
ASP_Volume3 Miscellaneous #54436

A Very Simple Sort

A very simpile example of sorting. Not the most efficeint, but easy for beginners to see whats happening.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Private Sub Form_Load()
  Dim a(10)
  a(0) = 2
  a(1) = 5
  a(2) = 7
  a(3) = 6
  a(4) = 13
  a(5) = "b"
  a(6) = 65
  a(7) = 0
  a(8) = 4
  a(9) = "a"
  a(10) = 1000
  
  Sort a, False
  
  For i = 0 To 10
    Debug.Print a(i)
  Next i
End Sub
Sub Sort(ByRef Arr() As Variant, Optional ByVal bAsc As Boolean = True)
  Dim Done As Boolean
  Done = False
  Do While Done = False
    Done = True
    For i = 0 To UBound(Arr) - 1
      If (Arr(i) > Arr(i + 1) And bAsc) Or (Arr(i) < Arr(i + 1) And Not bAsc) Then Swap Arr(i), Arr(i + 1): Done = False
    Next i
  Loop
End Sub
Sub Swap(ByRef a As Variant, ByRef b As Variant)
  Dim tmp As Variant
  tmp = a
  a = b
  b = tmp
End Sub
Original Comments (3)
Recovered from Wayback Machine