PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Swapping Integers-VBForums

1. ## 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

2. ## Re: Swapping Integers

Sort them how? in assending order? and if they are userdefined can you show the type definition.

3. ## 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

4. ## 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 IntegerDim Tmp As IntegerDim i As IntegerDim 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`

5. ## Re: Swapping Integers

I have a question..how do u do it without using a temp variable?

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.

7. ## 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

8. ## 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.

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
•

Featured