Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. Based on the type of search operation, these algorithms are generally classified into two categories: 搜索算法是检查或检索任何数据结构中的一个元素。根据搜索问题的类别,这些算法通常分为两个类别。
- Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For example: Linear Search. 顺序查找:顺序遍历list或array,检查每个元素。比如线性查找。
- Interval Search: These algorithms are specifically designed for searching in sorted data-structures. These type of searching algorithms are much more efficient than Linear Search as they repeatedly target the center of the search structure and divide the search space in half. For Example: Binary Search. 区间查找:针对的是排序好的数据。比线性查找更有效率,因为不断与数据的中间值比较,然后分半查找。
Linear Search to find the element “20” in a given list of numbers
Binary Search to find the element “23” in a given list of numbers
Searching Algorithms :
- Linear Search 线性查找
- Binary Search 二分查找
- Jump Search 跳越搜寻
- Interpolation Search 插值寻找
- Exponential Search 指数搜索
- Sublist Search (Search a linked list in another list) 子list搜索
- Fibonacci Search 斐波那契查找
- The Ubiquitous Binary Search 无处不在的二分搜索
- Recursive program to linearly search an element in a given array 对给定数组中元素进行线性搜索的递归程序
- Recursive function to do substring search 递归函数做子串搜索
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time) 无界二分搜索实例
1 Linear Search线性搜索
Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[]. 给定一个数组,写一个函数搜索给定的值。
Examples :
Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 110; Output : 6 Element x is present at index 6 Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 175; Output : -1 Element x is not present in arr[].
A simple approach is to do linear search, i.e
- Start from the leftmost element of arr[] and one by one compare x with each element of arr[] 从arr的最左开始,一个一个比较搜索。
- If x matches with an element, return the index. 如果匹配,则返回
- If x doesn’t match with any of elements, return -1. 不匹配任何优势,返回-1.
Example:
// C++ code for linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
#include <iostream>
using namespace std;
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
(result == -1)? cout<<"Element is not present in array"
: cout<<"Element is present at index " <<result;
return 0;
}
// c code for linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
#include <stdio.h>
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
(result == -1) ? printf("Element is not present in array")
: printf("Element is present at index %d",
result);
return 0;
}
// Java code for linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
class GFG
{
public static int search(int arr[], int x)
{
int n = arr.length;
for(int i = 0; i < n; i++)
{
if(arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int result = search(arr, x);
if(result == -1)
System.out.print("Element is not present in array");
else
System.out.print("Element is present at index " + result);
}
}
# Python3 code for linearly search x in arr[].
# If x is present then return its location,
# otherwise return -1
def search(arr, n, x):
for i in range (0, n):
if (arr[i] == x):
return i;
return -1;
# Driver Code
arr = [ 2, 3, 4, 10, 40 ];
x = 10;
n = len(arr);
result = search(arr, n, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result);
Output:
Element is present at index 3
The time complexity of above algorithm is O(n).
Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search. 线性查找很少用,因为二分搜索和hash表都特别快。
Also See – Binary Search
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
2 Binary Search二分搜索
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[]. 给定一个排序的数组,查找元素x。
A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another approach to perform the same task is using Binary Search.
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. 二分搜索:不断把排序好的数组分半,直到找到。如果查找目标小于区间的中间值,则缩小到右边一半,否则缩小到左边一半。
Example :
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).二分搜索的思想是把数组排序,降低时间复杂度到O(log(n))。
We basically ignore half of the elements just after one comparison.
- Compare x with the middle element.
- If x matches with middle element, we return the mid index.
- Else If x is greater than the mid element, then x can only lie in right half subarra