update translation linear-search (#36448)
added Swift, Java and PHP code examples
This commit is contained in:
committed by
Randell Dawson
parent
028477db00
commit
ae66b22295
@ -97,6 +97,50 @@ def linear_search(array, num):
|
||||
return -1
|
||||
```
|
||||
|
||||
### Exemplo em Swift
|
||||
```swift
|
||||
func linearSearch(for number: Int, in array: [Int]) -> Int? {
|
||||
for (index, value) in array.enumerated() {
|
||||
if value == number { return index } // return the index of the number
|
||||
}
|
||||
return nil // the number was not found in the array
|
||||
}
|
||||
```
|
||||
|
||||
### Exemplo em Java
|
||||
```java
|
||||
int linearSearch(int[] arr, int element)
|
||||
{
|
||||
for(int i=0;i<arr.length;i++)
|
||||
{
|
||||
if(arr[i] == element)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Exemplo em PHP
|
||||
|
||||
```php
|
||||
function linear_search($arr=[],$num=0)
|
||||
{
|
||||
$n = count($arr);
|
||||
for( $i=0; $i<$n; $i++){
|
||||
if($arr[$i] == $num)
|
||||
return $i;
|
||||
}
|
||||
// Item not found in the array
|
||||
return -1;
|
||||
}
|
||||
|
||||
$arr = array(1,3,2,8,5,7,4,0);
|
||||
print("Linear search result for 2: ");
|
||||
echo linear_search($arr,2);
|
||||
|
||||
```
|
||||
|
||||
## Pesquisa Linear Global
|
||||
|
||||
E se você estiver pesquisando as várias ocorrências de um elemento? Por exemplo, você quer ver quantos 5 estão em uma matriz.
|
||||
|
Reference in New Issue
Block a user