Searching Algorithms -- 搜索算法

本文详细介绍了几种常见的搜索算法:线性搜索、二分搜索、跳跃搜索、插值搜索和指数搜索。线性搜索适用于未排序的数据,时间复杂度为O(n);二分搜索适用于有序数据,效率较高,时间复杂度为O(log n);跳跃搜索在有序数组中跳跃查找,最佳步长为√n,时间复杂度为O(√n);插值搜索是改进版的二分搜索,适合均匀分布的数据;指数搜索首先确定元素可能存在的范围,再进行二分搜索,时间复杂度为O(log n)。
摘要由CSDN通过智能技术生成

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: 搜索算法是检查或检索任何数据结构中的一个元素。根据搜索问题的类别,这些算法通常分为两个类别。

  1. Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For example: Linear Search. 顺序查找:顺序遍历list或array,检查每个元素。比如线性查找。
  2. 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 :

  1. Linear Search 线性查找
  2. Binary Search 二分查找
  3. Jump Search 跳越搜寻
  4. Interpolation Search 插值寻找
  5. Exponential Search 指数搜索
  6. Sublist Search (Search a linked list in another list) 子list搜索
  7. Fibonacci Search 斐波那契查找
  8. The Ubiquitous Binary Search  无处不在的二分搜索
  9. Recursive program to linearly search an element in a given array 对给定数组中元素进行线性搜索的递归程序
  10. Recursive function to do substring search 递归函数做子串搜索
  11. 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.

  1. Compare x with the middle element.
  2. If x matches with middle element, we return the mid index.
  3. Else If x is greater than the mid element, then x can only lie in right half subarra
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值