By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

help me for array in vb6

P: 2
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
Nov 9 '06 #1
Share this Question
Share on Google+
5 Replies


Expert 5K+
P: 8,434
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.
Nov 9 '06 #2

100+
P: 1,646
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub SortArray(ByRef ar() As Long)
  2.     Dim temp As Long
  3.     Dim lngOuterLoop As Long
  4.     Dim lngInnerLoop As Long
  5.  
  6.     For lngOuterLoop = 0 To UBound(ar)
  7.         For lngInnerLoop = 0 To UBound(ar) - 1
  8.             If ar(lngInnerLoop) > ar(lngInnerLoop + 1) Then
  9.                 temp = ar(lngInnerLoop)
  10.                 ar(lngInnerLoop) = ar(lngInnerLoop + 1)
  11.                 ar(lngInnerLoop + 1) = temp
  12.             End If
  13.         Next lngInnerLoop
  14.     Next lngOuterLoop
  15. End Sub
  16.  
Nov 10 '06 #3

Expert 5K+
P: 8,434
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 :).
Nov 11 '06 #4

100+
P: 1,646
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.
Nov 12 '06 #5

Expert 5K+
P: 8,434
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.
Nov 12 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.