[algogeeks] Re: swap objects without temp variable

2012-11-19 Thread AISHWARYA KUMAR
in one line a=a^b^(b=a) ; --

Re: [algogeeks] Re: swap objects without temp variable

2012-11-19 Thread abhinav gupta
we need to use + and - as operator overloading. so, that i will work f9 for ur objects. so make operator overloading for + and - operator. On Sat, Nov 17, 2012 at 9:12 PM, AISHWARYA KUMAR aish@gmail.com wrote: in one line a=a^b^(b=a) ; -- -- *Thanks and

Re: [algogeeks] Re: swap objects without temp variable

2012-11-19 Thread Gaurav Gulzar
one liner a^=b^=a^=b On Mon, Nov 19, 2012 at 4:30 PM, abhinav gupta abhinav@gmail.comwrote: we need to use + and - as operator overloading. so, that i will work f9 for ur objects. so make operator overloading for + and - operator. On Sat, Nov 17, 2012 at 9:12 PM, AISHWARYA

[algogeeks] Re: swap objects without temp variable

2012-11-15 Thread DHAARAA
Can anyone please help me to understand (with example) that how those solutions work, if they are OBJECTS? On Sunday, November 4, 2012 3:32:43 PM UTC-5, manish wrote: Swapping two objects (not integers/chars),without using temp...? my solution is using xor operation..is that right and ny

[algogeeks] Re: swap objects without temp variable

2012-11-14 Thread Dave
@Don: This can be easily fixed, as there is no need to swap equal values, so: void swap(int a, int b) { if( a != b ) { a ^= b; b ^= a; a ^= b; } } On Monday, November 5, 2012 10:41:42 AM UTC-6, Don wrote: Note that most of these methods fail if you try to

[algogeeks] Re: swap objects without temp variable

2012-11-13 Thread Don
Dave is right. The code is undefined, which means that a valid compiler could produce code which makes monkeys fly out of your nose. On Nov 13, 12:38 am, Dave dave_and_da...@juno.com wrote: @Shivam: Your one-line solution violates the sequence point rule. Hence, it is non-standard, and the

[algogeeks] Re: swap objects without temp variable

2012-11-05 Thread Don
Note that most of these methods fail if you try to swap an item with itself. For example, swap(a[i], a[j]) will fail if i==j and swap is implemented as void swap(int a, intb) { a ^= b; b ^= a; a ^= b; } Don On Nov 4, 3:32 pm, manish narayan.shiv...@gmail.com wrote: Swapping two

Re: [algogeeks] Re: swap objects without temp variable

2012-11-05 Thread Umer Farooq
But how is that going to work for objects? On Mon, Nov 5, 2012 at 6:43 AM, Ashok Varma verma@gmail.com wrote: Try this: a = a + b - (b = a); //single line code to swap On Mon, Nov 5, 2012 at 4:53 AM, Dave dave_and_da...@juno.com wrote: @Manish: Sure. a = a + b; b = a - b; a = a -

[algogeeks] Re: swap objects without temp variable

2012-11-04 Thread Dave
@Manish: Sure. a = a + b; b = a - b; a = a - b; In 2-s complement arithmetic, it works even if a + b overflows. Dave On Sunday, November 4, 2012 2:32:43 PM UTC-6, manish wrote: Swapping two objects (not integers/chars),without using temp...? my solution is using xor operation..is that