|
| 1 | +package easyq; |
| 2 | +/* BY-M.NAVEEN |
| 3 | + * engineerscodes |
| 4 | + * code -Program for array rotation |
| 5 | + * sr.code-Ar5 |
| 6 | + * moves zeros to end of the array |
| 7 | + * */ |
| 8 | +import java .util.*; |
| 9 | +public class rotatesearch // class name |
| 10 | +{ |
| 11 | + static Scanner nav=new Scanner(System.in); // calling Scanner and creating object nav |
| 12 | + static int a[],size,element,count=0,flag=0; // class instances variables |
| 13 | + public rotatesearch(int c) // constructor |
| 14 | + { size=c; |
| 15 | + a=new int[size]; //intialization of variables |
| 16 | + } |
| 17 | + |
| 18 | + public static void rsearch() //method to do right_rotation and then search |
| 19 | + { int temp=a[0]; |
| 20 | + for(int i=1;i<size;++i) //rotation by 1 index |
| 21 | + { |
| 22 | + a[i-1]=a[i]; |
| 23 | + |
| 24 | + } |
| 25 | + count++; |
| 26 | + a[size-1]=temp; |
| 27 | + System.out.println(Arrays.toString(a)); |
| 28 | + if (a[0]==element) //search at the position zero if element is present |
| 29 | + { |
| 30 | + flag=1; |
| 31 | + return; |
| 32 | + |
| 33 | + } |
| 34 | + if(count==size) //if count is equal to size then return to main because u allready traversed whole array |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + else |
| 39 | + rsearch(); //recursive call |
| 40 | + |
| 41 | + } |
| 42 | + public static void input() //array input method |
| 43 | + { |
| 44 | + for(int i=0;i<a.length;i++) |
| 45 | + a[i]=nav.nextInt(); |
| 46 | + System.out.println("element in array are::"+Arrays.toString(a)); |
| 47 | + System.out.println("enter value to find value"); |
| 48 | + element=nav.nextInt(); |
| 49 | + rsearch(); |
| 50 | + if(flag==0) |
| 51 | + System.out.println("element is not found in array"); |
| 52 | + else |
| 53 | + System.out.println("element is found in array"); |
| 54 | + } |
| 55 | +public static void main(String arg[]) //main method |
| 56 | +{ |
| 57 | + System.out.println("enter the size of the array"); |
| 58 | + rotatesearch n=new rotatesearch(nav.nextInt()); |
| 59 | + System.out.println("Enter the element of array"); |
| 60 | + input(); //input method |
| 61 | +} |
| 62 | +} |
0 commit comments