Results 1 to 2 of 2

Thread: sorting arrays with selection sort

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    2

    Question

    Can anyone help me with this problem?
    I have to sort the random numbers in lstVelg in lstSorter
    by using selection sort.

    Thank you!

    Option Explicit
    Option Base 1 'setter nedre grense til 1


    Private Sub cmdExit_Click()
    End 'avslutter program
    End Sub

    Private Sub cmdSorter_Click()

    End Sub

    Private Sub cmdVelg_Click()
    Dim teller As Integer
    Dim Tall(10) As Integer


    lstVelg.Clear 'tømmer lista for innhold
    lstSorter.Clear 'tømmer lista for innhold

    'velger 10 tall
    For teller = 1 To 10 'teller is counter
    Randomize
    Tall(teller) = 1 + Int(1000 * Rnd())'tall is number
    lstVelg.AddItem Tall(teller)

    Next teller



    End Sub

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    Option Base 1 'setter nedre grense til 1
    
    Sub SortNum(iArray As Variant)
         
         Dim lLoop1 As Long
         Dim lLoop2 As Long
         Dim lTemp As Long
         
         For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
           For lLoop2 = LBound(iArray) + 1 To lLoop1
    
             If iArray(lLoop2 - 1) > iArray(lLoop2) Then
               lTemp = iArray(lLoop2 - 1)
               iArray(lLoop2 - 1) = iArray(lLoop2)
               iArray(lLoop2) = lTemp
             End If
           Next lLoop2
         Next lLoop1
       End Sub
    Private Sub cmdExit_Click()
    Unload Me
          
    'End 'avslutter program
    End Sub
    
    Private Sub cmdSorter_Click()
    
    End Sub
    
    Private Sub cmdVelg_Click()
        Dim teller As Integer
        Dim Tall(10) As Integer
        Dim myArr() 'array for information
        
        lstVelg.Clear 'tømmer lista for innhold
        lstSorter.Clear 'tømmer lista for innhold
        
        'velger 10 tall
        For teller = 1 To 10 'teller is counter
        ReDim Preserve myArr(teller)
        
        Randomize
        Tall(teller) = (1 + Int(1000 * Rnd())) 'tall is number
        lstVelg.AddItem Tall(teller)
        myArr(teller) = Tall(teller)
        
        Next teller
    'sort the array and load it in lstSorter
        Call SortNum(myArr)
        For teller = LBound(myArr) To UBound(myArr)
          lstSorter.AddItem myArr(teller)
        Next teller
        
        End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width