help me for array in vb6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kazijaveed
    New Member
    • Oct 2006
    • 2

    #1

    help me for array in vb6

    Hi

    My question is i have declared two arrays,when i enter values it shuld be store in first array after that the values of first array comes to second array with sorting iknow sorted function but i want go through code please send me details of my problem i dont have code for this

    thanks
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by kazijaveed
    Hi
    My question is i have declared two arrays,when i enter values it shuld be store in first array after that the values of first array comes to second array with sorting iknow sorted function but i want go through code please send me details of my problem i dont have code for this
    thanks
    The simplest wau would be to just copy the values to the second array (in a FOR loop, I suppose) and then sort them.

    The simplest sort to code is a "bubble" sort. To do this, you just loop through the array and compare each value with the one after it. If they are the wrong way around, swap them. Do this repeatedly until there is nothing to swap.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Originally posted by kazijaveed
      Hi

      My question is i have declared two arrays,when i enter values it shuld be store in first array after that the values of first array comes to second array with sorting iknow sorted function but i want go through code please send me details of my problem i dont have code for this

      thanks
      This is a bubble sort:
      Code:
      Private Sub SortArray(ByRef ar() As Long)
          Dim temp As Long
          Dim lngOuterLoop As Long
          Dim lngInnerLoop As Long
          
          For lngOuterLoop = 0 To UBound(ar)
              For lngInnerLoop = 0 To UBound(ar) - 1
                  If ar(lngInnerLoop) > ar(lngInnerLoop + 1) Then
                      temp = ar(lngInnerLoop)
                      ar(lngInnerLoop) = ar(lngInnerLoop + 1)
                      ar(lngInnerLoop + 1) = temp
                  End If
              Next lngInnerLoop
          Next lngOuterLoop
      End Sub

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by willakawill
        This is a bubble sort:
        ...
        Thanks willakawill. I didn't have the time to post code the other day. One thing, it's a bit more efficient if you limit the loops so you only go through the entries that are actually necessary.

        Um... it's been a while since I played with this, but from memory, I used to keep track of the last swap performed, then end the inner loop at that point, next time around.

        Since you've pushed the values as far as you can in one direction, there's no need to go beyond that point in future iterations, as you know there's nothing there to swap.

        In fact, I used to code a bi-directional variant. It simply "bubbles" through in one direction, then the other, then back the other way again. That way, you can set both upper and lower limits, and cut down the looping much more quickly.

        Of course, you can just find some sort of callable class, or sample code, for a faster sort I suppose. Bubble is one of the slowest available. Not the slowest, though. A year or two back I came across a website which demonstrated a bunch of different sort routines. One of them, I forget what they called it, but it effectively performed random swaps until it found the items in sequence. The amount of time it would take to sort a given number of items could range from the shortest theoretically possible time for any sort, to infinity :).

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by Killer42
          Thanks willakawill. I didn't have the time to post code the other day. One thing, it's a bit more efficient if you limit the loops so you only go through the entries that are actually necessary.

          Um... it's been a while since I played with this, but from memory, I used to keep track of the last swap performed, then end the inner loop at that point, next time around.

          Since you've pushed the values as far as you can in one direction, there's no need to go beyond that point in future iterations, as you know there's nothing there to swap.

          In fact, I used to code a bi-directional variant. It simply "bubbles" through in one direction, then the other, then back the other way again. That way, you can set both upper and lower limits, and cut down the looping much more quickly.

          Of course, you can just find some sort of callable class, or sample code, for a faster sort I suppose. Bubble is one of the slowest available. Not the slowest, though. A year or two back I came across a website which demonstrated a bunch of different sort routines. One of them, I forget what they called it, but it effectively performed random swaps until it found the items in sequence. The amount of time it would take to sort a given number of items could range from the shortest theoretically possible time for any sort, to infinity :).
          I use a quick sort routine which does not seem appropriate to post here for this question. Too difficult to understand how it works with recursion etc. Better to stick to the bubble when someone is just learning.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by willakawill
            I use a quick sort routine which does not seem appropriate to post here for this question. Too difficult to understand how it works with recursion etc. Better to stick to the bubble when someone is just learning.
            Yet another item to add to the repository, if we ever create one. A decent sort routine is the kind of thing that belongs in every toolkit. Quicksort is probably the best generally available, though as you say it can be tough to follow. Certainly I've never understood it, though I have used it.

            Comment

            Working...