# Data structure and algorithm -- search algorithm

catalogue

1, Search algorithm

1.1 classification

2, Linear search (SequenceSearch)

2.1 basic ideas

2.2 implementation of linear search algorithm

3, Binary search

3.1 basic ideas

3.2 implementation of binary search algorithm

4, Interpolation search

4.1 basic ideas

4.2 expected index value formula

4.3 implementation of interpolation search algorithm

5, Fibonacci search

5.1 basic introduction

5.2 basic ideas

five point three   Expected index value formula

5.4 implementation of Fibonacci search algorithm

## 1, Search algorithm

### 1.1 classification

• Linear search
• Binary search
• Interpolation lookup
• fibonacci search

## 2, Linear search (SequenceSearch)

### 2.1 basic ideas

Compare the sequence to be sorted one by one to find out the subscript of the element to be found.

### 2.2 implementation of linear search algorithm

code:

```public class SequenceSearch {
public static void main(String[] args) {
int arr[] = {8, 9, 1, 7, 2, 3, 5, 4, 6, -1};
int index = search(arr, 2);
if (index == -1) {
System.out.println("The element does not exist in the sequence.");
} else {
System.out.println("The subscript of this element is:" + index);
}
}

public static int search(int[] arr, int value) {
//Compare the sequence to be sorted one by one
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
}```

result: ## 3, Binary search

### 3.1 basic ideas

First determine the middle subscript of the ordered sequence, and then recursively perform binary search to the left and right until the middle subscript is the subscript of the element to be searched.

### 3.2 implementation of binary search algorithm

code:

```public class BinarySearch {
public static void main(String[] args) {
int arr[] = {-1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int index = search(arr, 0, arr.length - 1, 5);
if (index == -1) {
System.out.println("The element does not exist in the sequence.");
} else {
System.out.println("The subscript of this element is:" + index);
}
}

public static int search(int[] arr, int startIndex, int endIndex, int value) {
if (startIndex > endIndex || value < arr || value > arr[arr.length - 1]) {
return -1;
}
//Define intermediate element subscript
int mid = (startIndex + endIndex) / 2;
//Define intermediate element values
int midVal = arr[mid];

if (value > midVal) {
//Recursive right
return search(arr, mid + 1, endIndex, value);
} else if (value < midVal) {
//Left recursion
return search(arr, startIndex, mid - 1, value);
} else {
return mid;
}
}
}```

result: ## 4, Interpolation search

### 4.1 basic ideas

It is similar to binary search, but changes the expected index value of each search.

### 4.2 expected index value formula

Expectation = startIndex + (endIndex - startIndex) * (value - arr[startIndex]) / (arr[endIndex] - arr[startIndex])

### 4.3 implementation of interpolation search algorithm

code:

```public class InterpolationSearch {
public static void main(String[] args) {
int arr[] = {-1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int index = search(arr, 0, arr.length - 1, 5);
if (index == -1) {
System.out.println("The element does not exist in the sequence.");
} else {
System.out.println("The subscript of this element is:" + index);
}
}

public static int search(int[] arr, int startIndex, int endIndex, int value) {
if (startIndex > endIndex || value < arr || value > arr[arr.length - 1]) {
return -1;
}
//Define expected element subscript
int expectation = startIndex + (endIndex - startIndex) * (value - arr[startIndex]) / (arr[endIndex] - arr[startIndex]);
//Define expected element values
int expectationVal = arr[expectation];

if (value > expectationVal) {
//Recursive right
return search(arr, expectationVal + 1, endIndex, value);
} else if (value < expectationVal) {
//Left recursion
return search(arr, startIndex, expectationVal - 1, value);
} else {
return expectationVal;
}
}
}
```

result: ## 5, Fibonacci search

### 5.1 basic introduction

Fibonacci search algorithm is also called golden section search algorithm. Golden section point refers to dividing a line segment into two parts, so that the ratio of one part to the full length is equal to that of the other part, and the approximate value of the ratio is 0.618.

The sequence in which the ratio of two adjacent elements is infinitely close to the golden section value is called Fibonacci sequence.

### 5.2 basic ideas

It is similar to binary search, but changes the expected index value of each search, which is located near the golden section point.

### five point three   Expected index value formula

Expectation = startIndex + F(k - 1) - 1, f represents Fibonacci sequence.

• From the properties of Fibonacci sequence F(k) = F(k - 1) + F(k - 2), we can get: (F(k) - 1) = (F(k - 1) - 1) + (F(k - 2) - 1) + 1. This formula shows that as long as the length of the ordered sequence is F(k) - 1, the sequence can be divided into two parts with length of F(k - 1) - 1 and F(k - 2) - 1, and the subscript of the expected index value is startIndex + F(k - 1) - 1.
• When the length of the ordered sequence is not F(k) - 1, the length of the sequence needs to be increased to F(k) - 1.

### 5.4 implementation of Fibonacci search algorithm

code:

```public class FibonacciSearch {

//Defines the length of the Fibonacci sequence
public static int maxSize = 20;

public static void main(String[] args) {
int arr[] = {-1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int index = search(arr, 5);
if (index == -1) {
System.out.println("The element does not exist in the sequence.");
} else {
System.out.println("The subscript of this element is:" + index);
}
}

public static int search(int[] arr, int value) {
int startIndex = 0;
//Define tail pointer
int endIndex = arr.length - 1;
//Define expected element subscript
int expectation = 0;
//Subscript that defines the Fibonacci split value
int k = 0;
//Define Fibonacci sequence
int[] f = fibonacci();
//Get Fibonacci segmentation value
while (endIndex > f[k] - 1) {
k++;
}
//If the length of the ordered sequence is insufficient, increase its length and store it in the temporary array. The insufficient part is filled with the value at the end of the arr array
int[] temp = Arrays.copyOf(arr, f[k]);
for (int i = endIndex + 1; i < temp.length; i++) {
temp[i] = arr[endIndex];
}

while (startIndex <= endIndex) {
expectation = startIndex + f[k - 1] - 1;
//Define expected element values
int expectationVal = arr[expectation];

if (value > expectationVal) {
//Find right
startIndex = expectation + 1;
//The ordered sequence on the right has f[k-2] elements, which can be divided into f[k-2]=f[k-3]+f[k-4], and k-2 is used as the subscript of the New Fibonacci division value
k -= 2;
} else if (value < expectationVal) {
//Find left
endIndex = expectation - 1;
//The ordered sequence on the left has f[k-1] elements, which can be split into f[k-1]=f[k-2]+f[k-3], and k-1 is used as the subscript of the New Fibonacci division value
k--;
} else {
//Judge whether the found element subscript is in the added part. If yes, return the subscript at the end of the array
if (expectation <= endIndex) {
return expectation;
} else {
return endIndex;
}
}
}
return -1;
}

//Get Fibonacci sequence
public static int[] fibonacci() {
int[] f = new int[maxSize];
f = 1;
f = 1;
for (int i = 2; i < maxSize; i++) {
f[i] = f[i - 1] + f[i - 2];
}
return f;
}
}```

result: Posted on Sat, 04 Dec 2021 21:56:15 -0500 by jimthunderbird