Swapping
There are many ways to handle a swap operation; moreover,
people have reason to believe that bitwise swapping is faster and more
efficient than other methods; however, I strongly disagree with them. Only with the oldest microprocessor, people can
have their desire effectiveness using bitwise operation but in modern processor
architecture, bitwise operations can perform as faster as adding or subtracting
operation. Consequently, for bitwise
operators like XOR(^) have more operations than simple addition or subtraction ;
as a result , it seems that addition is the best way to go.
Please save the file in (.cpp) format for Code::Blocks, Code:
/* |
Author : Alim Ul Karim |
|------------------------------------------|
| Code tested on CodeBlocks , GCC Compiler
|
| Example of Swapping in C/C++, Save in .cpp |
|------------------------------------------|
*/
#include
<stdio.h>
//using third variable
void swap_based_on_address(int *a, int *b){
if ( *a == *b ){
return;}
int temp =
*a;
*a
= *b;
*b
= temp;
}
//using third variable
void swap_based_on_address(int &a, int &b){
if ( a == b
){
return;}
int temp =
a;
a = b;
b = temp;
}
//using without using third
variable, XOR(Bitwise Swapping)
void swap_using_xor(int &a, int &b){
//no
need to change if a=b, and this
condition is must for XOR
if ( a == b
){
return;}
a = a ^
b;
b = a ^
b;
a = a ^
b;
}
//using without using third
variable, using addition and subtraction
void swap_using_add(int &a, int &b){
//no need to change if a=b
if ( a == b
){
return;}
a = a +
b;
b = a -
b;
a = a -
b;
}
//using without using third
variable, using multiplication , division
void swap_using_division(int &a, int &b){
//no need to change if a=b
if(
a == b ){
return;}
a = a * b;
b = a /
b;
a = a /
b;
}
int main(){
int a =
10 , b = 12;
int backup_a = a , backup_b = b;
swap_based_on_address(&a , &b);
printf("swapping using memory location :\nFrom(a=%d,b=%d) to a=%d , b=%d\n\n"
, backup_a
, backup_b ,
a ,b);
a = backup_a, b = backup_b;
swap_based_on_address(a , b); // save file as a .cpp format
printf("swapping using memory location by sending only value
to a function :\nFrom(a=%d,b=%d) to a=%d
, b=%d\n\n"
, backup_a , backup_b , a ,b);
a = backup_a, b = backup_b;
swap_using_xor(a , b); // save file as a .cpp format
printf("Swap using XOR :\nFrom(a=%d,b=%d)
to a=%d , b=%d\n\n" , backup_a , backup_b ,
a ,b);
a = backup_a, b = backup_b;
swap_using_add(a , b); // save file as a .cpp format
printf("Swap using addition, subtraction :\nFrom(a=%d,b=%d) to a=%d , b=%d\n\n"
,
backup_a ,
backup_b , a ,b);
a = backup_a, b = backup_b;
swap_using_division(a , b); // save file as a .cpp format
printf("Swap using multiplication, division :\nFrom(a=%d,b=%d) to a=%d , b=%d\n\n"
,
backup_a ,
backup_b , a ,b);
return 0;
}
|
----------------------------------------------------------------------------------------------------------------------------------
Search Engine Friendly Links:
-
Swap variable using XOR
-
Swap variable using addition(+)
-
Swap variable without the third
-
Swap variable using bitwise operator
-
Swapping variable using addition(+)
-
Swapping variable without the third
-
Swapping numbers without third variable
-
Swapping variable without the third in C
-
Swap variable using addition(+) in C++
-
Swap variables using addition(+) without the third variable
-
Swapping numbers without third variable using addition(+)
-
Swap number variable without third variable using addition(+)
No comments:
Post a Comment