Sort Array of integers? VB5 Pro Ed.(sp2) 
Author Message
 Sort Array of integers? VB5 Pro Ed.(sp2)

 I need to be able to sort an array of variables (integers) so that I can
print them as a string in the sorted order.  Can anyone please help with the
sorting?

Thanks,
--
Tony Freeman



Fri, 25 Feb 2000 03:00:00 GMT  
 Sort Array of integers? VB5 Pro Ed.(sp2)

On Mon, 8 Sep 1997 13:10:33 -0500, "Tony Freeman"

Quote:

>< I need to be able to sort an array of variables (integers) so that I can
><print them as a string in the sorted order.  Can anyone please help with the
><sorting?
><
><Thanks,
><--
><Tony Freeman

><
><
><
><

If the simple bubble sort doesn't satisfy you, you can find answers to
your question in Knuth's "Sorting And Searching".


Fri, 25 Feb 2000 03:00:00 GMT  
 Sort Array of integers? VB5 Pro Ed.(sp2)

Tony:

Here's a simple bubble sort:

    For i = Last To First Step -1
      For j = First + 1 To i
        If Value(j - 1) > Value(j) Then
          tmp = Value(j - 1)
          Value(j - 1) = Value(j)
          Value(j) = tmp
        End If
      Next
    Next

Hope this helps.



Quote:

>  I need to be able to sort an array of variables (integers) so that I can
> print them as a string in the sorted order.  Can anyone please help with
the
> sorting?

> Thanks,
> --
> Tony Freeman




Fri, 25 Feb 2000 03:00:00 GMT  
 Sort Array of integers? VB5 Pro Ed.(sp2)

Quote:

> I need to be able to sort an array of variables (integers) so that I can
>print them as a string in the sorted order.  Can anyone please help with the
>sorting?
>Thanks,
>--
>Tony Freeman


Hi Tony,

First of all, I'm a VB3 user, so I hope the following will be of use.

The principle I've used is to read, write or generate an unsorted
array and copy the array to a SORTED list box, and then overwrite the
contents of the listbox onto the original array. The code is
deliberately as longwinded as possible so that you can follow what's
going on.

Paste the following code into the (general) declarations of a default
form.
THEN paste a Command Button and a List Box onto the form
- you MUST set the List Box's Sorted Property to TRUE!

--------------------------------------

Dim a(0 To 9)  As Integer

Sub Command1_Click ()

'Start Random Number Generator
Randomize

'Create Random Integers
'and copy to array
For i = 0 To 9
a(i) = Int(Rnd * 20)
Next i

'Print array onto form
Print "Unsorted Integers"
For i = 0 To 9
Print a(i)
Next i

'Copy array to SORTED list box
'Note: the format$ is important.
'The listbox is normally set to Visible = FALSE
For i = 0 To 9

Next i

'Read the list box and re-copy to array
For i = 0 To list1.ListCount - 1
a(i) = list1.List(i)
Next i

'Visual seperator
Print

'Print sorted array
Print "Sorted Integers"
For i = 0 To 9
Print a(i)
Next i

End Sub

------------------------------------

Besta luck,


----------------
P133 - 16 meg.
Mail - Eudora
News - Free Agent
-----------------
My fellow Webians,
Ask NOT what your web can do for you,
Ask what YOU can do for your web.
-----------------



Sat, 26 Feb 2000 03:00:00 GMT  
 Sort Array of integers? VB5 Pro Ed.(sp2)

Quote:

> If the simple bubble sort doesn't satisfy you, you can find answers to
> your question in Knuth's "Sorting And Searching".

Another good "bible" on algorithms is "Introduction to Algorithms" by
Cormen, Leiserson, Rivest - MIT Press.

/M



Sat, 26 Feb 2000 03:00:00 GMT  
 Sort Array of integers? VB5 Pro Ed.(sp2)

Quote:


> > If the simple bubble sort doesn't satisfy you, you can find answers to
> > your question in Knuth's "Sorting And Searching".
> Another good "bible" on algorithms is "Introduction to Algorithms" by
> Cormen, Leiserson, Rivest - MIT Press.
> /M

If the domain is small, use an array of counts of each integer.
Otherwise, I recommend binsort or a radix sort. I haven't seen
an O(n) sort beat yet!

--

WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Sat, 26 Feb 2000 03:00:00 GMT  
 Sort Array of integers? VB5 Pro Ed.(sp2)

Quote:

> I need to be able to sort an array of variables (integers) so that I can
>print them as a string in the sorted order.  Can anyone please help with the
>sorting?
>Thanks,
>--
>Tony Freeman


Hi Tony,

First of all, I'm a VB3 user, so I hope the following will be of use.

The principle I've used is to read, write or generate an unsorted
array and copy the array to a SORTED list box, and then overwrite the
contents of the listbox onto the original array. The code is
deliberately as longwinded as possible so that you can follow what's
going on.

Paste the following code into the (general) declarations of a default
form.
THEN paste a Command Button and a List Box onto the form
- you MUST set the List Box's Sorted Property to TRUE!

--------------------------------------

Dim a(0 To 9)  As Integer

Sub Command1_Click ()

'Start Random Number Generator
Randomize

'Create Random Integers
'and copy to array
For i = 0 To 9
a(i) = Int(Rnd * 20)
Next i

'Print array onto form
Print "Unsorted Integers"
For i = 0 To 9
Print a(i)
Next i

'Copy array to SORTED list box
'Note: the format$ is important.
'The listbox is normally set to Visible = FALSE
For i = 0 To 9

Next i

'Read the list box and re-copy to array
For i = 0 To list1.ListCount - 1
a(i) = list1.List(i)
Next i

'Visual seperator
Print

'Print sorted array
Print "Sorted Integers"
For i = 0 To 9
Print a(i)
Next i

End Sub

------------------------------------

Besta luck,


----------------
P133 - 16 meg.
Mail - Eudora
News - Free Agent
-----------------
My fellow Webians,
Ask NOT what your web can do for you,
Ask what YOU can do for your web.
-----------------



Thu, 09 Mar 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Wanted VB5.0 Pro Ed.

2. Sorting a database in VB3 Pro Ed

3. Installation of VB5 Pro ED

4. VB 5.0 Professional Ed. suddenly becomes VB 5.0 Learning Ed.

5. VB5 Learn Ed./Access Database/Password/ODBC

6. DBengine in VB5 Leaning Ed.

7. VB5 Learning Ed. and the Web

8. VB5 Learning Ed.

9. VB5 ENT ED. SP-2

10. Converting integers and long integers to byte arrays

 

 
Powered by phpBB® Forum Software