问题:给定n个元素的数组arr [],编写一个函数以搜索arr []中的给定元素x。
例子 :
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[].
一种简单的方法是进行线性搜索,即
- 从arr []的最左边的元素开始,然后将x与arr []的每个元素一一比较
- 如果x与元素匹配,则返回索引。
- 如果x与任何元素都不匹配,则返回-1。
例子:
C++
// C++ code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
#include
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;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
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
// C code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
#include
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
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
// Java code for linearly searching 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;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
// Function call
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
# Python3 code to 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)
# Function call
result = search(arr, n, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)
C#
// C# code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
using System;
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;
}
// Driver code
public static void Main()
{
int[] arr = { 2, 3, 4, 10, 40 };
int x = 10;
// Function call
int result = search(arr, x);
if (result == -1)
Console.WriteLine(
"Element is not present in array");
else
Console.WriteLine("Element is present at index "
+ result);
}
}
// This code is contributed by DrRoot_
PHP
Javascript
C++14
// C++ program for linear search
#include
using namespace std;
void search(vector arr, int search_Element)
{
int left = 0;
int length = arr.size();
int position = -1;
int right = length - 1;
// Run loop from 0 to right
for(left = 0; left <= right;)
{
// If search_element is found with
// left variable
if (arr[left] == search_Element)
{
position = left;
cout << "Element found in Array at "
<< position + 1 << " Position with "
<< left + 1 << " Attempt";
break;
}
// If search_element is found with
// right variable
if (arr[right] == search_Element)
{
position = right;
cout << "Element found in Array at "
<< position + 1 << " Position with "
<< length - right << " Attempt";
break;
}
left++;
right--;
}
// If element not found
if (position == -1)
cout << "Not found in Array with "
<< left << " Attempt";
}
// Driver code
int main()
{
vector arr{ 1, 2, 3, 4, 5 };
int search_element = 5;
// Function call
search(arr, search_element);
}
// This code is contributed by mayanktyagi1709
Java
// Java program for linear search
import java.io.*;
class GFG
{
public static void search(int arr[], int search_Element)
{
int left = 0;
int length = arr.length;
int right = length - 1;
int position = -1;
// run loop from 0 to right
for (left = 0; left <= right;)
{
// if search_element is found with left variable
if (arr[left] == search_Element)
{
position = left;
System.out.println(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (left + 1) + " Attempt");
break;
}
// if search_element is found with right variable
if (arr[right] == search_Element)
{
position = right;
System.out.println(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (length - right) + " Attempt");
break;
}
left++;
right--;
}
// if element not found
if (position == -1)
System.out.println("Not found in Array with "
+ left + " Attempt");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int search_element = 5;
// Function call
search(arr,search_element);
}
}
Python3
# Python3 program for linear search
def search(arr, search_Element):
left = 0
length = len(arr)
position = -1
right = length - 1
# Run loop from 0 to right
for left in range(0, right, 1):
# If search_element is found with
# left variable
if (arr[left] == search_Element):
position = left
print("Element found in Array at ", position +
1, " Position with ", left + 1, " Attempt")
break
# If search_element is found with
# right variable
if (arr[right] == search_Element):
position = right
print("Element found in Array at ", position + 1,
" Position with ", length - right, " Attempt")
break
left += 1
right -= 1
# If element not found
if (position == -1):
print("Not found in Array with ", left, " Attempt")
# Driver code
arr = [1, 2, 3, 4, 5]
search_element = 5
# Function call
search(arr, search_element)
# This code is contributed by Dharanendra L V.
C#
// C# program for linear search
using System;
class GFG
{
public static void search(int []arr,
int search_Element)
{
int left = 0;
int length = arr.Length;
int right = length - 1;
int position = -1;
// run loop from 0 to right
for (left = 0; left <= right;)
{
// if search_element is found with left variable
if (arr[left] == search_Element)
{
position = left;
Console.WriteLine(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (left + 1) + " Attempt");
break;
}
// if search_element is found with right variable
if (arr[right] == search_Element)
{
position = right;
Console.WriteLine(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (length - right) + " Attempt");
break;
}
left++;
right--;
}
// if element not found
if (position == -1)
Console.WriteLine("Not found in Array with "
+ left + " Attempt");
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int search_element = 5;
// Function call
search(arr,search_element);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
Element is present at index 3
上述算法的时间复杂度为O(n)。
线性搜索实际上很少使用,因为其他搜索算法(例如二进制搜索算法和哈希表)可实现与线性搜索相比更快的搜索比较。
改善线性搜索最坏情况的复杂性
- 如果元素最后在O(n)到O(1)处找到
- 如果未找到元素O(n)至O(n / 2)
下面是实现:
C++ 14
// C++ program for linear search
#include
using namespace std;
void search(vector arr, int search_Element)
{
int left = 0;
int length = arr.size();
int position = -1;
int right = length - 1;
// Run loop from 0 to right
for(left = 0; left <= right;)
{
// If search_element is found with
// left variable
if (arr[left] == search_Element)
{
position = left;
cout << "Element found in Array at "
<< position + 1 << " Position with "
<< left + 1 << " Attempt";
break;
}
// If search_element is found with
// right variable
if (arr[right] == search_Element)
{
position = right;
cout << "Element found in Array at "
<< position + 1 << " Position with "
<< length - right << " Attempt";
break;
}
left++;
right--;
}
// If element not found
if (position == -1)
cout << "Not found in Array with "
<< left << " Attempt";
}
// Driver code
int main()
{
vector arr{ 1, 2, 3, 4, 5 };
int search_element = 5;
// Function call
search(arr, search_element);
}
// This code is contributed by mayanktyagi1709
Java
// Java program for linear search
import java.io.*;
class GFG
{
public static void search(int arr[], int search_Element)
{
int left = 0;
int length = arr.length;
int right = length - 1;
int position = -1;
// run loop from 0 to right
for (left = 0; left <= right;)
{
// if search_element is found with left variable
if (arr[left] == search_Element)
{
position = left;
System.out.println(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (left + 1) + " Attempt");
break;
}
// if search_element is found with right variable
if (arr[right] == search_Element)
{
position = right;
System.out.println(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (length - right) + " Attempt");
break;
}
left++;
right--;
}
// if element not found
if (position == -1)
System.out.println("Not found in Array with "
+ left + " Attempt");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int search_element = 5;
// Function call
search(arr,search_element);
}
}
Python3
# Python3 program for linear search
def search(arr, search_Element):
left = 0
length = len(arr)
position = -1
right = length - 1
# Run loop from 0 to right
for left in range(0, right, 1):
# If search_element is found with
# left variable
if (arr[left] == search_Element):
position = left
print("Element found in Array at ", position +
1, " Position with ", left + 1, " Attempt")
break
# If search_element is found with
# right variable
if (arr[right] == search_Element):
position = right
print("Element found in Array at ", position + 1,
" Position with ", length - right, " Attempt")
break
left += 1
right -= 1
# If element not found
if (position == -1):
print("Not found in Array with ", left, " Attempt")
# Driver code
arr = [1, 2, 3, 4, 5]
search_element = 5
# Function call
search(arr, search_element)
# This code is contributed by Dharanendra L V.
C#
// C# program for linear search
using System;
class GFG
{
public static void search(int []arr,
int search_Element)
{
int left = 0;
int length = arr.Length;
int right = length - 1;
int position = -1;
// run loop from 0 to right
for (left = 0; left <= right;)
{
// if search_element is found with left variable
if (arr[left] == search_Element)
{
position = left;
Console.WriteLine(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (left + 1) + " Attempt");
break;
}
// if search_element is found with right variable
if (arr[right] == search_Element)
{
position = right;
Console.WriteLine(
"Element found in Array at "
+ (position + 1) + " Position with "
+ (length - right) + " Attempt");
break;
}
left++;
right--;
}
// if element not found
if (position == -1)
Console.WriteLine("Not found in Array with "
+ left + " Attempt");
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int search_element = 5;
// Function call
search(arr,search_element);
}
}
// This code is contributed by 29AjayKumar
Java脚本
输出
Element found in Array at 5 Position with 1 Attempt
另请参阅–二进制搜索