考虑一个问题,其中可能一次又一次地搜索相同的元素。有效地执行搜索操作。
例子 :
Input : arr[] = {12 25 36 85 98 75 89 15 63 66
64 74 27 83 97}
q[] = {63, 63, 86, 63, 78}
Output : Yes Yes No Yes No
We need one by one search items of q[] in arr[].
The element 63 is present, 78 and 86 are not present.
这个想法很简单,我们将搜索到的元素移到数组的前面,以便下次可以快速搜索它。
C++
// C++ program to implement search for an item
// that is searched again and again.
#include
using namespace std;
// A function to perform sequential search.
bool search(int arr[], int n, int x)
{
// Linearly search the element
int res = -1;
for (int i = 0; i < n; i++)
if (x == arr[i])
res = i;
// If not found
if (res == -1)
return false;
// Shift elements before one position
int temp = arr[res];
for (int i = res; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
return true;
}
// Driver Code
int main()
{
int arr[] = { 12, 25, 36, 85, 98, 75, 89, 15,
63, 66, 64, 74, 27, 83, 97 };
int q[] = {63, 63, 86, 63, 78};
int n = sizeof(arr)/sizeof(arr[0]);
int m = sizeof(q)/sizeof(q[0]);
for (int i=0; i
Java
// Java program to implement search for an item
// that is searched again and again.
import java.util.*;
class solution
{
// A function to perform sequential search.
static boolean search(int[] arr, int n, int x)
{
// Linearly search the element
int res = -1;
for (int i = 0; i < n; i++)
if (x == arr[i])
res = i;
// If not found
if (res == -1)
return false;
// Shift elements before one position
int temp = arr[res];
for (int i = res; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
return true;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 12, 25, 36, 85, 98, 75, 89, 15,
63, 66, 64, 74, 27, 83, 97 };
int []q = {63, 63, 86, 63, 78};
int n = arr.length;
int m = q.length;
for (int i=0; i
Python3
# Python 3 program to implement search for
# an item that is searched again and again.
# A function to perform sequential search.
def search(arr, n, x):
# Linearly search the element
res = -1
for i in range(0, n, 1):
if (x == arr[i]):
res = i
# If not found
if (res == -1):
return False
# Shift elements before
# one position
temp = arr[res]
i = res
while(i > 0):
arr[i] = arr[i - 1]
i -= 1
arr[0] = temp
return True
# Driver Code
if __name__ == '__main__':
arr = [12, 25, 36, 85, 98, 75, 89,
15, 63, 66, 64, 74, 27, 83, 97]
q = [63, 63, 86, 63, 78]
n = len(arr)
m = len(q)
for i in range(0, m, 1):
if(search(arr, n, q[i])):
print("Yes", end = " ")
else:
print("No", end = " ")
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to implement search for an
// item that is searched again and again.
using System;
class GFG
{
// A function to perform sequential search.
static bool search(int[] arr, int n, int x)
{
// Linearly search the element
int res = -1;
for (int i = 0; i < n; i++)
if (x == arr[i])
res = i;
// If not found
if (res == -1)
return false;
// Shift elements before one position
int temp = arr[res];
for (int i = res; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
return true;
}
// Driver Code
public static void Main()
{
int[] arr = { 12, 25, 36, 85, 98, 75, 89, 15,
63, 66, 64, 74, 27, 83, 97 };
int[] q = {63, 63, 86, 63, 78};
int n = arr.Length;
int m = q.Length;
for (int i = 0; i < m; i++)
{
if(search(arr, n, q[i]) == true)
Console.Write("Yes ");
else
Console.Write("No ");
}
}
}
// This code is contributed by
// Akanksha Rai
PHP
0; $i--)
$arr[$i] = $arr[$i - 1];
$arr[0] = $temp;
return true;
}
// Driver Code
$arr = array(12, 25, 36, 85, 98, 75, 89, 15,
63, 66, 64, 74, 27, 83, 97);
$q = array(63, 63, 86, 63, 78);
$n = sizeof($arr);
$m = sizeof($q);
for ($i = 0; $i < $m; $i++)
if(search($arr, $n, $q[$i]))
echo "Yes ";
else
echo "No ";
// This code is contributed
// by Akanksha Rai
输出:
Yes Yes No Yes No
进一步考虑:通过使用链接列表,我们可以做得更好。在链接列表中,将项目移到最前面可以在O(1)时间内完成。
最好的解决方案是使用Splay Tree(为此目的而设计的数据结构)。 Splay树平均支持O(Log n)时间的插入,搜索和删除操作。此外,展开树是BST,因此我们可以按排序顺序快速打印元素。