Click here to Skip to main content
Click here to Skip to main content

Cost Reduced Swap

, 9 Dec 2006
Rate this:
Please Sign up or sign in to vote.
Swapping in C++

Introduction

void main() 
{ 
    int i=10,j=20; 
    j=i+j-(i=j); 
} 

Here the two variables are swapped in a single statement . The cost is reduced than the known swapping methods.

  • Temporary variables are not used.
  • addition and subtraction only.
  • can be used for any data types and this is efficient than the normal methods.
  • time is saved.
  • space is saved.
  • complexity reduced.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

jeyakumarrr

India India
i am an Computer Engineering student from india

Comments and Discussions

 
GeneralTradeoff between efficiency and maintainability PinmemberHarold Bamford12-Dec-06 8:46 
Questionwhat about asm ? Pinmemberbruno leclerc11-Dec-06 23:35 
Questionwhat about asm ? Pinmemberbruno leclerc11-Dec-06 23:33 
GeneralThe "classic" way PinmemberLuca Piccarreta11-Dec-06 4:49 
GeneralNot Guaranteed to Work Pinmemberricecake11-Dec-06 4:23 
Generalexpression is evaluated from left to right Pinmemberjeyakumarrr11-Dec-06 0:24 
Generalit will work in Turbo C& C++ compiler only Pinmemberjeyakumarrr11-Dec-06 0:23 
GeneralDoes not work! Pinmemberz33z10-Dec-06 7:29 
Generalit will work for all data types.. Pinmemberjeyakumarrr9-Dec-06 23:48 
Generalcheck it out in the system and say Pinmemberjeyakumarrr9-Dec-06 23:42 
QuestionUndefined behaviour? Pinmemberdavid_dawkins9-Dec-06 15:22 
GeneralIt doesn't work PinmemberVindicator6669-Dec-06 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 9 Dec 2006
Article Copyright 2006 by jeyakumarrr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid