Click here to Skip to main content
Licence 
First Posted 8 Dec 2006
Views 22,982
Bookmarked 11 times

Cost Reduced Swap

By | 8 Dec 2006 | Article
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

About the Author

jeyakumarrr



India India

Member

i am an Computer Engineering student from india

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTradeoff between efficiency and maintainability PinmemberHarold Bamford7:46 12 Dec '06  
Questionwhat about asm ? Pinmemberbruno leclerc22:35 11 Dec '06  
Questionwhat about asm ? Pinmemberbruno leclerc22:33 11 Dec '06  
GeneralThe "classic" way PinmemberLuca Piccarreta3:49 11 Dec '06  
GeneralNot Guaranteed to Work Pinmemberricecake3:23 11 Dec '06  
Generalexpression is evaluated from left to right Pinmemberjeyakumarrr23:24 10 Dec '06  
Generalit will work in Turbo C& C++ compiler only Pinmemberjeyakumarrr23:23 10 Dec '06  
GeneralDoes not work! Pinmemberz33z6:29 10 Dec '06  
Generalit will work for all data types.. Pinmemberjeyakumarrr22:48 9 Dec '06  
Generalcheck it out in the system and say Pinmemberjeyakumarrr22:42 9 Dec '06  
QuestionUndefined behaviour? Pinmemberdavid_dawkins14:22 9 Dec '06  
GeneralIt doesn't work PinmemberVindicator6663:45 9 Dec '06  

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.

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