“Pencarian linier” Kode Jawaban

Pencarian linier

#include <bits/stdc++.h>

using namespace std; 

int search(int arr[], int n, int key) 
{ 
    int i; 
    for (i = 0; i < n; i++) 
        if (arr[i] == key) 
            return i; 
    return -1; 
} 

int main() 
{ 
    int arr[] = { 99,4,3,8,1 }; 
    int key = 8; 
    int n = sizeof(arr) / sizeof(arr[0]); 

    int result = search(arr, n, key); 
    (result == -1) 
        ? cout << "Element is not present in array"
        : cout << "Element is present at index " << result; 

    return 0; 
}
Glamorous Gibbon

Apa itu pencarian linier

A linear search is the simplest method of searching a data set. Starting at the beginning of the data set, each item of data is examined until a match is made. Once the item is found, the search ends.
Nasty Newt

Algoritma pencarian linier

const linearSearch = (arr, item) => {
  for (const i in arr) {
    if (arr[i] === item) return +i;
  }
  return -1;
};
Amuda Andrew-oid

Implementasi Pencarian Linier

def linear_search(lst, target):
    """Returns the index position of the target if found, else returns -1"""

    for i in range(0, len(lst)):
        if lst[i] == target:
            return i
    return -1
Agreeable Anaconda

Jawaban yang mirip dengan “Pencarian linier”

Pertanyaan yang mirip dengan “Pencarian linier”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya