Pages

Oct 20, 2011

Swapping Algorithms in C/C++



Swapping Algorithms in C/C++




             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               |
   | Email      : auk.junk@live.com           |
   | Portfolio  : auk-port.webs.com           |
   | Blog       : bit.ly/auk-blog             |
   |------------------------------------------|
   | 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(){
     // save file as a .cpp format
    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:

Author:

Search Engine Friendly Labels:

Swapping ,

Swap,

Swap using addition and subtraction,

Swapping using addition and subtraction ,

Swapping using addition without the third variable ,

Swapping numbers in C ,

Swapping variables in C ,

Swapping variables in C++ ,

Swap variables in C++ ,

Swap variables in C,

Swapping without using the third varible in C ,

Swap variable using bitwise operator ,

Swap variable using XOR operator ,

Swap variable using division operator ,

Swap variable using multiplication operator ,

Swap variable using XOR (^) operator ,

Swap XOR ,

XOR(^)

Swapping without third variable

Swapping/Swap without using third variable in C/C++





It was never been so hard to swap number variables without using third variable, however there are many ways to do it but it seems that swapping  variables using addition and subtraction operators are the best way to go.
For instance, assume two number variables ‘a’ and ‘b’

a = 10; b = 12;

Now, if a = a + b; then ‘a’ would be 22. Hence, ‘a’ is a combination of ‘a’ and ‘b’ , so if ‘b’ is subtracted from ‘a’ , it will return the value of ‘a’.

a = a + b; // summation of both 22
b = a – b; // remain the ‘a’, which we are taking in b. 22 – 12 = 10
a = a – b; // remain the ‘b’, which we are taking in b. 22 – 10 = 12

Now we can see the code for swapping variables using addition (+) and subtraction (-) operator without using the third variable:

(See the code very carefully how pointers in functions can help to use the memory location to swap)

/* | Author     : Alim Ul Karim               |
   | Email      : auk.junk@live.com           |
   | Portfolio  : auk-port.webs.com           |
   | Blog       : bit.ly/auk-blog             |
   |------------------------------------------|
   | Code tested on CodeBlocks , GCC Compiler |
   | Example of Swapping in C/C++, Save in .cpp       |
   |------------------------------------------|
 */


#include <stdio.h>

//using without using third variable, using addition and subtraction , save as .cpp file
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;
}

int main(){
    // save as .cpp file
    int a = 10 , b = 12;
    int backup_a = a , backup_b = 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);


    return 0;
}

----------------------------------------------------------------------------------------------------------------------------------

Search Engine Friendly Links:

Author:



Search Engine Friendly Labels:

Swapping ,

Swap,

Swap using addition and subtraction,

Swapping using addition and subtraction ,

Swapping using addition without the third variable ,

Swapping numbers in C ,

Swapping variables in C ,

Swapping variables in C++ ,

Swap variables in C++ ,

Swap variables in C,

Swapping without using the third varible in C

Aug 9, 2011

Clean up the Screen in C/C++ Using CLS

How to Clean up the Screen in C/C++



How to Clean up the Screen in C/C++

To clean up your C/C++ screen you can use system("cls"). It is very understandable that some compilers does not support cls/CLS and clrscr()



/* | Author     : Alim Ul Karim               |
   | Email      : auk.junk@live.com           |
   | Portfolio  : auk-port.webs.com           |
   | Blog       : bit.ly/auk-blog             |
   |------------------------------------------|
   | Code tested on CodeBlocks , GCC Compiler |
   | Example of cls or clean up screen        |
   |------------------------------------------|
 */


#include <stdio.h>

int main(){
    printf("Before system(cls)\n");
    system("cls");
    printf("after system(cls)\n");
}

Author Alim Ul Karim's Related Links:
Related Links :
SEO LABELS:

Clean Screen In C/C++ , 

CLS in C/C++ ,  

system("cls") in C , s

ystem("cls") in C++, Cl

eanScreen in c, 

clean the screen in C/C++