-
Jul 8th, 2005, 05:10 AM
#1
Thread Starter
Member
Swapping Integers
i have an array with 5 integers (that are user defined). I would like to sort them how can i do that??
All i need to know is how to sort 5 integers in an array
Thanx in Advanced Darcey
Last edited by darcey_123; Jul 8th, 2005 at 09:15 PM.
Reason: {RESOLVED}
-
Jul 8th, 2005, 05:39 AM
#2
Ex-Super Mod'rater
Re: Swapping Integers
Sort them how? in assending order? and if they are userdefined can you show the type definition.
When your thread has been resolved please edit the original post in the thread ( )
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
-
Jul 8th, 2005, 05:40 AM
#3
Thread Starter
Member
Re: Swapping Integers
ok.....i want them in ascending order...however im not sure wat "Type Definition" means is that like integer...because if so then yeah they are integers
-
Jul 8th, 2005, 05:56 AM
#4
Ex-Super Mod'rater
Re: Swapping Integers
Ow right thought you meant it was a user-defined-type.
Right well the following code should work...
VB Code:
Dim Nums(0 To 4) As Integer
Dim Tmp As Integer
Dim i As Integer
Dim j As Integer
Nums(0) = 67
Nums(1) = 22
Nums(2) = 4
Nums(3) = 66
Nums(4) = 7
For i = 0 To 4
For j = i To 4
If Nums(i) > Nums(j) Then
Tmp = Nums(j)
Nums(j) = Nums(i)
Nums(i) = Tmp
End If
Next
Next
When your thread has been resolved please edit the original post in the thread ( )
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
-
Oct 28th, 2007, 09:07 PM
#5
New Member
Re: Swapping Integers
I have a question..how do u do it without using a temp variable?
-
Oct 28th, 2007, 09:19 PM
#6
Re: Swapping Integers
Edited. Must be getting tired. Thought I read with a swap, without a swap?
Int1=Int1 Xor Int2
Int2=Int2 Xor Int1
Int1=Int1 Xor Int2
Other than using a swap variable or some other variable, unless you can teach your PC to juggle variables in mid-air, there really isn't a shortcut.
Last edited by LaVolpe; Oct 28th, 2007 at 09:38 PM.
-
Oct 28th, 2007, 09:33 PM
#7
Hyperactive Member
Re: Swapping Integers
For i = 0 To 4, For j = i To 4
could be
For i = 0 To 3 For j = i+1 To 4
10 compares as opposed to 15.
Mac
Last edited by Mr.Mac; Oct 28th, 2007 at 09:39 PM.
-
Oct 28th, 2007, 10:54 PM
#8
New Member
Re: Swapping Integers
Originally Posted by LaVolpe
Edited. Must be getting tired. Thought I read with a swap, without a swap?
Int1=Int1 Xor Int2
Int2=Int2 Xor Int1
Int1=Int1 Xor Int2
Other than using a swap variable or some other variable, unless you can teach your PC to juggle variables in mid-air, there really isn't a shortcut.
thanks.
-
Oct 29th, 2007, 01:40 AM
#9
Re: Swapping Integers
Of course the compiler may optimize the use of a temporary variable. I'm not sure it'll try that with the XOR swap since both locations are indexed array elements - but it may.
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
|