Thursday, July 3, 2014

Array - II


Swapping and shifting two things always needed in a program specially in the program in the arrays. Here is some way one can swap and shift.


Swapping



import java.io.*;
    class Swapping
        {
            public static void main(String args[])throws IOException
                {
                    Swapping S=new Swapping();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b,c;
                    System.out.print("\n\tEnter First No ");
                    a=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tEnter Second No ");
                    b=Integer.parseInt(Br.readLine());
                    System.out.print("\n\tBefore Swapping, a= " + a+"  b = "+b);
                    S.Swap1(a,b);
                    S.Swap2(a,b);
                    S.Swap3(a,b);
                    S.Swap4(a,b);
                    //S.Left_Shift();
                    //S.Right_Shift();
                }
            void Swap1(int a,int b)
                {
                    //Using third variable, not advisable
                    int c;
                    c=a;
                    a=b;
                    b=c;
                    System.out.print("\n\tAfter Swapping, a= " + a+"  b = "+b);
                 }
            void Swap2(int a,int b)
                {
                    //Without third variable
                    a=a+b;
                    b=a-b;
                    a=a-b;
                    System.out.print("\n\tAfter Swapping, a= " + a+"  b = "+b);
                 }
            void Swap3(int a,int b)
                {
                    //One line implementation of above program
                    a=a+b-(b=a);
                    System.out.print("\n\tAfter Swapping, a= " + a+"  b = "+b);
                 }
            void Swap4(int a,int b)
                {
                    //Using bit operator ^ (XOR)
                    a=a^b;
                    b=a^b;
                    a=a^b;
                    System.out.print("\n\tAfter Swapping, a= " + a+"  b = "+b);
                 }                
                }         


Output



          Enter First No 5

          Enter Second No 57

          Before Swapping, a= 5  b = 57
          After Swapping, a= 57  b = 5
          After Swapping, a= 57  b = 5
          After Swapping, a= 57  b = 5
          After Swapping, a= 57  b = 5




Shifting

In Array we cannot delete, however we did it by shifting, overwriting and not displaying the values we want to delete. Here is two example of Left Shit to delete first element and right shift to dele last element.



import java.io.*;
    class Shifting
        {
            public static void main(String args[])throws IOException
                {
                    Shifting S=new Shifting();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int Size;
                    System.out.print("\n\tEnter Size of Array :  ");
                    Size=Integer.parseInt(Br.readLine());
                    int Ar[]=new int[Size];
                    int Ar1[]=new int[Size];
                     for(int i=0;i<Size;i++)
                        {
                            System.out.print("\tEnter Element at ["+(i+1)+"]");
                            Ar[i]=Integer.parseInt(Br.readLine());
                            Ar1[i]=Ar[i];
                        }
                    System.out.print("\n\tBefore Shifting \t");
                    for(int i=0;i<Size;i++)
                        {
                            System.out.print("  "+Ar[i]);
                        }
                    S.Left_Shift(Ar,Size);
                    S.Right_Shift(Ar1,Size);
                }
            void Left_Shift(int Ar[], int Size)
                {
                    for(int i=0;i<Size-1;i++)
                        {
                            Ar[i]=Ar[i+1];
                        }
                    Size--;   
                    System.out.print("\n\tAfter Left Shift \t");
                    for(int i=0;i<Size;i++)
                        {
                            System.out.print("  "+Ar[i]);
                        }
                }
            void Right_Shift(int Ar[], int Size)
                {
                    for(int i=Size-1;i>0;i--)
                        {
                            Ar[i]=Ar[i-1];
                           
                        }
                    System.out.print("\n\tAfter Right Shift \t");
                    for(int i=1;i<Size;i++)
                        {
                            System.out.print("  "+Ar[i]);
                        }
                }
                
      }         


Output



          Enter Size of Array :  5
          Enter Element at [1]2
          Enter Element at [2]1
          Enter Element at [3]5
          Enter Element at [4]6
          Enter Element at [5]9

          Before Shifting         2  1  5  6  9
          After Left Shift         1  5  6  9
          After Right Shift        2  1  5  6






No comments: