Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. package com.search.demo;
  2.  
  3. public class FibonacciSearch {
  4.  
  5. static int[] a = {10,20,30,40,50,60,70,80,90,100};
  6.  
  7. static int required = 70;
  8. static int m = 2;
  9. static int p = 0;
  10. static int q = 0;
  11.  
  12.  
  13. /**
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. // TODO Auto-generated method stub
  18. FibonacciSearch fs = new FibonacciSearch();
  19. fs.findm();
  20. fibSearch(required);
  21. }
  22.  
  23. private void findm(){
  24. //here you have to find Fm which matches size of searching array, or which is close to it.
  25. int n = a.length;
  26. int fibCurrent = 1;
  27. int fibPrev1 = 1;
  28. int fibPrev2 = 0;
  29.  
  30. while(n > fibCurrent){
  31. fibPrev2 = fibPrev1;
  32. fibPrev1 = fibCurrent;
  33. fibCurrent = fibPrev1 + fibPrev2;
  34. m++;
  35. }
  36. p = m-1;
  37. q = m-2;
  38. }
  39.  
  40. public static int fibSearch(int no){
  41. for(;;){
  42.  
  43. if(m == 0){
  44. System.out.println("not found");
  45. return -1;
  46. }
  47. int j = f(p);
  48.  
  49. if(no == a[j]){
  50. System.out.println("found at "+p);
  51. }else if(no < a[j]){
  52. m = p;
  53. p = m - 1;
  54. q = m - 2;
  55.  
  56. }else if(no > a[j]){
  57. m = q; // as per the step 6..
  58. p = m-1;
  59. q = m-2;
  60. }
  61. }
  62. //return m;
  63. }
  64.  
  65. public static int f(int val){
  66.  
  67. if(val == 2 || val == 1 || val == 0){
  68. return 1;
  69. }
  70. return (f(val-1) + f(val-2));
  71. }
  72.  
  73.  
  74. }
  75.  
  76. while(n > fibCurrent){
  77. fibPrev2 = fibPrev1;
  78. fibPrev1 = fibCurrent;
  79. fibCurrent = fibPrev1 + fibPrev2;
  80. m++;
  81. }
  82.  
  83. package com.search.demo;
  84.  
  85. public class FibonacciSearch {
  86.  
  87. int a[] = {10,20,30,40,50,60,70,80,90,100};
  88. static FibonacciSearch fs;
  89.  
  90. /**
  91. * @param args
  92. */
  93. public static void main(String[] args) {
  94. // TODO Auto-generated method stub
  95. fs = new FibonacciSearch();
  96. int location = fs.find(70);
  97. if(location < 0){
  98. System.out.println("number not found..");
  99. }else{
  100. System.out.println("found at location "+location);
  101. }
  102. }
  103.  
  104. private int find(int no){
  105. int n = a.length;
  106. int m = findFm(n); //m = Fm iff n is Fibonacci number else returns Fm+1
  107. int p = fibSequenceIterative(m-1); //p = Fm-1, always a fibonacci number
  108. int q = fibSequenceIterative(m -2); //q = Fm-2, always a fibonacci number
  109.  
  110. while(true){
  111. if(no == a[m]){
  112. return m;
  113. }else if (no < a[m]){
  114. if(q == 0){
  115. return -(m - 1);// we crossed 0th index in array, number not found.
  116. }
  117. m = m - q; //moved to 1 step left towards a fibonacci num
  118. int tmp = p;//hold this temporarily
  119. p = q; //move p to 1 step left into another fibonacci num
  120. q = tmp - q;//moved q to 1 step left....
  121. }else if(no > a[m]){
  122. if(p == 1){
  123. return -m;//we reached 0th index in array again and number not found..
  124. }
  125. m = m + q;
  126. p = p - q;
  127. q = q - p;
  128. }
  129. }
  130.  
  131. }
  132.  
  133. private int findFm(int n){
  134. int prev = 1;
  135. int curr = 1;
  136. int next = 0;
  137.  
  138. if(n == 0){
  139. next = 0;
  140. return -1;
  141. }else if(n == 1 || n == 2){
  142. next = 1;
  143. return 1;
  144. }else{
  145. for(int i = 3; ; i++){
  146. next = prev + curr;
  147. prev = curr;
  148. curr = next;
  149. System.out.println("prev = "+prev+" curr = "+curr+" next = "+next);
  150. if(n <= curr){
  151. System.out.println("n = "+n+" curr = "+curr);
  152. return i;
  153. }
  154. }
  155. //return -1;//we should not get here..
  156. }
  157.  
  158.  
  159. }
  160.  
  161. /* Iterative method for printing Fibonacci sequence..*/
  162. private int fibSequenceIterative(int n){
  163. int prev = 1;
  164. int curr = 1;
  165. int next = 0;
  166.  
  167. if(n == 0){
  168. next = 0;
  169. //return 0;
  170. }else if(n == 1 || n == 2){
  171. next = 1;
  172. //return 1;
  173. }else{
  174. for(int i = 3; i <= n; i++){
  175. next = prev + curr;
  176. prev = curr;
  177. curr = next;
  178. }
  179. return next;
  180. }
  181. return next;
  182. }
  183.  
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement