|
-
Nov 26th, 2000, 10:44 PM
#1
Thread Starter
New Member
I'm really lost on the selection sort part of my project. My problem is with the sort. I got this form earlier:
for c = 1 to 10
for n = 0 to 8
If Val(a(n))>Val(a(n+1))
b = 0
b = a(n)
a(n) = Val(a(n+1))
a(n+1) = b
end if
next n
next c
What does val and b represent? Is b like temp? Now I know C is the 10 random numbers, a is the arrayname, and n is the array index(I think).
This is what I have for that part:
Private Sub cmdLoadArray_Click()
For i = 1 To 10
RandomNumber = Int(100 * Rnd) + 1
txtOriginal.Text = txtOriginal.Text & " " & RandomNumber
Next i
End Sub
So would A be txtOriginal.text and C be RandomNumber? Also since there are 6 spaces between the random numbers, would the first number that comes up be on index 6?
-
Nov 27th, 2000, 12:14 AM
#2
Frenzied Member
Before you can sort your string of numbers seperated by spaces, you'll have to use the Split function (available in VB6, if you're using a previous version then it's easy enough to make your own Split function) to split up your string into an array of smaller strings with your numbers in.
What you have there looks more like a bubble sort than a selection sort. In a bubble sort you swap adjacent numbers if they are in the wrong order, and you keep going through the array until there are no more swaps needed. With a selection sort, firt you find the highest number and swap that with the number in position 1, then find the next highest number and swap that with position 2, etc etc.
I have written out code for a selection sort on more than one occasion in ths forum, so I suggest you search these forums for 'selection sort' using the search utility on the site if you want some free code. Bubble sort is easier, but you seem to have a (rather odd) version of that already.
If you need some help understanding it, paste your code into a reply to this thread (use code tags to format the code - put [code] and [/code] at either end of your code block) and say what you're having trouble with. It sounds like you're after a selection sort, but have found a bubble sort accidentally.
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|