Translate

In this tutorial, We are going to show swapping between two variable. This question is mostly asked into the interview and i was unable to tell the answer. So finally after searching on search engine and tried on self coding. I got the solution now, which we are showing below.

We can easily swap two variable value with PHP function like: -
for Exchanging value we can use different types of methods.
Example:



yogesh-kumar     // Answer will be

We can swap two variable with the XOR method to.

".$a;
echo "b ->".$b;
?>

Answer will be: -
a - 11111
b - 33333

We can swap two variable with the use of our logic to.




And using anther one is here: -

$a = $a * $b;
$b = $a / $b;
$a = $a / $b;
In the C Language we can easily execute our code like.
    #include 
    void main()
    {
    int a,b;

    printf("Enter Number One: a");

    scanf("%d",a);

    printf("Enter Number Two: b ");

    scanf("%d",b);

    printf(Value of a and b Before swap a=%d,b=%d"a,b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf(value of a and b After swap a=%d,b=%d"a,b);

    }
 
One Another executable code is written below.
#include 
    void main()
    {
        int x,y;
        printf("Enter First Number :");
        scanf("%d",x);
        printf("Enter Second Number 2: ");
        scanf("%d",y);
        printf(value of x and y before swapping is x=%d,y=%d"x,y);
        y=x+y-(x=y);
        printf(value of x and y after swapping is x=%d,y=%d"x,y);
    }

Now, We will learn how Swapping of two nibble in byte using c++ ?
Nibble is an aggregation of 4 bits that is made by octet. we always have 2 nibble in byte.

We can easily understand with the help of example: -
Hundred(100) is presented in 01100100 in 8 bits. There are 2 nibbles like - (0110) and (0100). But if we swap to both of them together, Then we will get (01000110) with the value of 70 into decimal.

For swapping of 2 nibble, we generally uses bitwise &, << and >> operators into coding. Showing one example in C program swapping of two nibble in byte.
#include 
 
unsigned char swap_Nibbles(unsigned char y)
{
    return ( (y & 0x0F)<<4 | (y & 0xF0)>>4 );
}
 
int main()
{
    unsigned char y = 100;
    printf("%u", swap_Nibbles(y));
    return 0;
}
Output: -
70

Explanation:
The Binary value of 100 is 01100100. So with this binary value we had split into 2 parts.
First of all, We made one function named - swap_Nibbles() that will receive an argument as a value. We used ((y & 0x0F)<<4) that gives us last 4 bits of Y variable and ((y & 0xF0)>>4) gives us first 4 bits.

Then we have written main() code and perform printf function to run the swap_Nibbles(y) function.

Now its time to learn swapping in Java: -

The above swapping code in c will not work in Java Technology. Till now, Java has not this type of parameters. But with the help of an array variable we can perform this task. In array, we pass the array and 2 index for swapping as 3 Parameters to work in Java. One Java code program is elaborating this all.

We can easily understand using Java BubbleSort Program: -
public class Bubble_Sort {
   // swap: interchange inside array
   static void swap(int[] a, int i, int j) {
      int t = a[i];
      a[i] = a[j];
      a[j] = t;
   }

   // bubbleSort: very short code, but ineffient
   static void bubble_Sort(int[] a) {
      for (;;) {
         boolean sorted = true;
         for (int i = 0; i < a.length - 1; i++)
            if (a[i] > a[i+1]) {
               sorted = false;
               swap(a, i, i + 1);
            }
         if (sorted) break;
      }
   }

   static void printArray(int[] a) {
      for (int i = 0; i < a.length; i++) {
         if (i % 4 == 0) System.out.println();
         System.out.print(a[i] + " \t");
      }
      System.out.println();
   }

   public static void main(String[] args) {
      int size = Integer.parseInt(args[0]);
      System.out.println("Bubble_Sort, size = " + size);
      int[] r = new int[size];
      for (int i = 0; i < size; i++)
   {
         r[i] = (int)(Math.random()*size*10 + 1);
         long start_Time =System.currentTimeMillis();
        }
      Bubble_Sort(r);
      System.out.println("Elapsed time(millis) "+ (System.currentTimeMillis()-start_Time));
      // printArray(r);
   }
}
If you still having any problem then feel free to contact us with the help of contact form or mail us at : - estuffcode(at)gmail(dot)com.
Happy Coding - Don't forget to write your feedback. Thanks.
Next
Newer Post
Previous
This is the last post.

0 comments:

Post a Comment

 
Top