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
Results 1 to 9 of 9

Thread: Swapping Integers

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    New South Wales, Australia
    Posts
    55

    Resolved 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}

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    New South Wales, Australia
    Posts
    55

    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. #4
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Swapping Integers

    Ow right thought you meant it was a user-defined-type.


    Right well the following code should work...
    VB Code:
    1. Dim Nums(0 To 4) As Integer
    2. Dim Tmp As Integer
    3. Dim i As Integer
    4. Dim j As Integer
    5.  
    6.     Nums(0) = 67
    7.     Nums(1) = 22
    8.     Nums(2) = 4
    9.     Nums(3) = 66
    10.     Nums(4) = 7
    11.  
    12.     For i = 0 To 4
    13.         For j = i To 4
    14.             If Nums(i) > Nums(j) Then
    15.                 Tmp = Nums(j)
    16.                 Nums(j) = Nums(i)
    17.                 Nums(i) = Tmp
    18.             End If
    19.         Next
    20.     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.

  5. #5
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Re: Swapping Integers

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

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,678

    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.

  7. #7
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    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.

  8. #8
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Re: Swapping Integers

    Quote 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. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,922

    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


Click Here to Expand Forum to Full Width