Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Circular Shift an Array Given Position


Code ResourceAuthor: mohitjava  

Difficulty Level: Beginner

Published: 7th Apr 2010   Read: 5566 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

You have to circular shift the array by given position by user and you use only a single array.

 


Sample Code
  1. import java.io.*;
  2. class circular
  3. {
  4.    public static void main(String []arg)
  5.     {
  6.        Console co=System.console();
  7.         int a[]=new int[10];
  8.           int no,i,b,temp,r,c;
  9.         System.out.println("enter the element in array");
  10.            for(i=0;i<a.length;i++)  
  11.                a[i]=Integer.parseInt(co.readLine());
  12.                
  13.                   System.out.println("enter the position ");
  14.                     no=Integer.parseInt(co.readLine());
  15.                       for(i=0;i<=(a.length-(no+1));i++)
  16.                         {
  17.                          temp=a[i+no];
  18.                           a[i+no]=a[i];
  19.                            a[i]=temp;
  20.                           }
  21.                         b=a.length;
  22.                          
  23.                          if(no==1||no==2||no==3||no==4)
  24.                          {
  25.                          
  26.                        r=b%no;
  27.                            switch(r)
  28.                            {
  29.                                 case 0:
  30.                                          break;
  31.                             case 1:
  32.                                  while(i<a.length-1)
  33.                                                {
  34.                                                   temp=a[b-1];
  35.                                                   a[b-1]=a[i];
  36.                                                   a[i]=temp;
  37.                                                       i++;
  38.                                                      
  39.                                                }
  40.                                                  break;
  41.                              case 9:
  42.                                    break;
  43.                              case 2:
  44.                                 System.out.print(i);
  45.                                  for(;i<(b-2);i++)
  46.                                  {
  47.                                
  48.                                         temp=a[i];
  49.                                         a[i]=a[i+2];
  50.                                         a[i+2]=temp;
  51.                                  }                            
  52.                                 break;
  53.                            }
  54.                          }                       
  55.                            else
  56.                            {
  57.                      
  58.                                 c=no;
  59.                                 while(no<=(b-1))
  60.                                 {
  61.                          
  62.                                 for(i=no;i>=(b-c)+1;i--)
  63.                                 {
  64.                                         temp=a[i];
  65.                                         a[i]=a[i-1];
  66.                                         a[i-1]=temp;
  67.                                 }
  68.                                     no++;
  69.                                     c--;
  70.                                 }              
  71.                                        
  72.                            }                                                  
  73.                       System.out.prinln(after circular shift an array");                        
  74.                        for(i=0;i<a.length;i++)
  75.                         System.out.println(a[i]+" ");
  76.  
  77.                                
  78.  
  79.            }                                      
  80.  
  81.  
  82. }
Copyright GeekInterview.com


Next Article: Fibonacci Series Generator


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora140703
2. iamdvr102872
3. venki_madesh35423
4. Raju20167
5. parmod kumar duhan16407
6. Kiran.jakkaraju15124
7. chowsys7786
8. ashish.cns6881
9. Venkateswara Rao6772
10. venkat_kiran5890

Active Coders

Refined Tags