Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite2a6b02

Browse files
authored
Merge pull requestTheAlgorithms#753 from abhijay94/Development
Added the algorithm and test for Fibonacci Search
2 parents0276ccd +09c4cd7 commite2a6b02

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
packagesrc.main.java.com.search;
2+
3+
importstaticjava.lang.Math.min;
4+
5+
/**
6+
* Fibonacci search is a method of searching a sorted array using a divide and conquer algorithm that narrows down
7+
* possible locations with the aid of Fibonacci numbers. Compared to binary search where the sorted array is divided
8+
* into two equal-sized parts, one of which is examined further, Fibonacci search divides the array into two parts that
9+
* have sizes that are consecutive Fibonacci numbers.
10+
* <p>
11+
* Worst-case performanceO(Log n)
12+
* Best-case performanceO(1)
13+
* Average performanceO(Log n)
14+
* Average space complexityO(1)
15+
*/
16+
publicclassFibonacciSearch {
17+
/**
18+
* @param array is an array where the element should be found
19+
* @param key is an element which should be found
20+
* @param <T> is any comparable type
21+
* @return The index position of the key in the array, returns -1 for empty array
22+
*/
23+
public <TextendsComparable<T>>intfindIndex(T[]array,Tkey) {
24+
intsize =array.length;
25+
26+
if (size ==0)
27+
return -1;
28+
29+
// Initialize the fibonacci numbers
30+
intfibN1 =1;// (n-1)th Fibonacci term
31+
intfibN2 =0;// (n-2)th Fibonacci term
32+
intfibN =fibN1 +fibN2;// nth Fibonacci term
33+
34+
// fibN should store the smallest Fibonacci Number greater than or equal to size
35+
while (fibN <size) {
36+
fibN2 =fibN1;
37+
fibN1 =fibN;
38+
fibN =fibN2 +fibN1;
39+
}
40+
41+
// Marks the eliminated range from front
42+
intoffset = -1;
43+
44+
while (fibN >1) {
45+
// Check if fibN2 is a valid location
46+
inti =min(offset +fibN2,size -1);
47+
48+
// If key is greater than the value at index fibN2, cuts the sub-array from offset to i
49+
if (array[i].compareTo(key) <0) {
50+
fibN =fibN1;
51+
fibN1 =fibN2;
52+
fibN2 =fibN -fibN1;
53+
offset =i;
54+
}
55+
56+
// If x is greater than the value at index fibN2, cuts the sub-array after i+1
57+
elseif (array[i].compareTo(key) >0) {
58+
fibN =fibN2;
59+
fibN1 =fibN1 -fibN2;
60+
fibN2 =fibN -fibN1;
61+
}elsereturni;// Element found
62+
}
63+
// comparing the last element with key
64+
if (fibN1 ==1 &&array[offset +1].compareTo(key) ==0)
65+
returnoffset +1;
66+
67+
return -1;// Element not found
68+
}
69+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagesrc.test.java.com.search;
2+
3+
importorg.junit.Assert;
4+
importorg.junit.Test;
5+
importsrc.main.java.com.search.FibonacciSearch;
6+
7+
publicclassFibonacciSearchTest {
8+
@Test
9+
publicvoidtestFibonacciSearch() {
10+
FibonacciSearchfibonacciSearch =newFibonacciSearch();
11+
12+
Integer[]arr = {11,14,23,32,36,40,54,69};
13+
intx =54;
14+
intindex =fibonacciSearch.findIndex(arr,x);
15+
Assert.assertEquals("Incorrect index",6,index);
16+
17+
Integer[]arrTwo = {-400, -283, -180, -160, -129, -120, -30};
18+
x = -120;
19+
index =fibonacciSearch.findIndex(arrTwo,x);
20+
Assert.assertEquals("Incorrect index",5,index);
21+
22+
String[]arrString = {"101","122","136","165","225","351","458"};
23+
StringstringX ="136";
24+
index =fibonacciSearch.findIndex(arrString,stringX);
25+
Assert.assertEquals("Incorrect index",2,index);
26+
27+
String[]arrThree = {};
28+
Assert.assertEquals("Incorrect index", -1,fibonacciSearch.findIndex(arrThree,""));
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp