Hướng dẫn how do you do a linear search in python? - làm cách nào để bạn thực hiện tìm kiếm tuyến tính trong python?

Trong hướng dẫn này, bạn sẽ tìm hiểu về tìm kiếm tuyến tính. Ngoài ra, bạn sẽ tìm thấy các ví dụ làm việc của tìm kiếm tuyến tính C, C ++, Java và Python.

Tìm kiếm tuyến tính là một thuật toán tìm kiếm tuần tự nơi chúng tôi bắt đầu từ một đầu và kiểm tra mọi yếu tố của danh sách cho đến khi tìm thấy phần tử mong muốn. Nó là thuật toán tìm kiếm đơn giản nhất.


Tìm kiếm tuyến tính hoạt động như thế nào?

Các bước sau đây được theo dõi để tìm kiếm một phần tử k = 1 trong danh sách bên dưới.

Hướng dẫn how do you do a linear search in python? - làm cách nào để bạn thực hiện tìm kiếm tuyến tính trong python?
Mảng sẽ được tìm kiếm
  1. Bắt đầu từ phần tử đầu tiên, so sánh k với mỗi phần tử x. So sánh với từng phần tử
    Hướng dẫn how do you do a linear search in python? - làm cách nào để bạn thực hiện tìm kiếm tuyến tính trong python?
    Compare with each element
  2. Nếu x == k, trả lại chỉ mục. Yếu tố tìm thấy
    Hướng dẫn how do you do a linear search in python? - làm cách nào để bạn thực hiện tìm kiếm tuyến tính trong python?
    Element found
  3. Khác, không tìm thấy trở lại.

Thuật toán tìm kiếm tuyến tính

LinearSearch(array, key)
  for each item in the array
    if item == value
      return its index

Ví dụ về Python, Java và C/C ++

# Linear Search in Python


def linearSearch(array, n, x):

    # Going through array sequencially
    for i in range(0, n):
        if (array[i] == x):
            return i
    return -1


array = [2, 4, 0, 1, 9]
x = 1
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
    print("Element not found")
else:
    print("Element found at index: ", result)

// Linear Search in Java

class LinearSearch {
  public static int linearSearch(int array[], int x) {
  int n = array.length;

  // Going through array sequencially
  for (int i = 0; i < n; i++) {
    if (array[i] == x)
    return i;
  }
  return -1;
  }

  public static void main(String args[]) {
  int array[] = { 2, 4, 0, 1, 9 };
  int x = 1;

  int result = linearSearch(array, x);

  if (result == -1)
    System.out.print("Element not found");
  else
    System.out.print("Element found at index: " + result);
  }
}

// Linear Search in C

#include <stdio.h>

int search(int array[], int n, int x) {
  
  // Going through array sequencially
  for (int i = 0; i < n; i++)
    if (array[i] == x)
      return i;
  return -1;
}

int main() {
  int array[] = {2, 4, 0, 1, 9};
  int x = 1;
  int n = sizeof(array) / sizeof(array[0]);

  int result = search(array, n, x);

  (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
}

// Linear Search in C++

#include <iostream>
using namespace std;

int search(int array[], int n, int x) {

  // Going through array sequencially
  for (int i = 0; i < n; i++)
    if (array[i] == x)
      return i;
  return -1;
}

int main() {
  int array[] = {2, 4, 0, 1, 9};
  int x = 1;
  int n = sizeof(array) / sizeof(array[0]);

  int result = search(array, n, x);

  (result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
}


Sự phức tạp tìm kiếm tuyến tính

Độ phức tạp về thời gian: O (n) O(n)

Độ phức tạp không gian: O(1) O(1)


Ứng dụng tìm kiếm tuyến tính

  1. Để tìm kiếm các hoạt động trong các mảng nhỏ hơn (

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc Given an array arr[] of n elements, write a function to search a given element x in arr[]. 

    Bàn luận

    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[].

    Vấn đề: Cho một mảng mảng [] của n phần tử, hãy viết một hàm để tìm kiếm một phần tử X đã cho trong ARR []. & NBSP;linear search, i.e

    • Ví dụ:
    • Một cách tiếp cận đơn giản là thực hiện tìm kiếm tuyến tính, tức là
    • Bắt đầu từ phần tử ngoài cùng bên trái của ARR [] và từng người một so sánh x với mỗi phần tử của ARR []

    Nếu x khớp với một phần tử, hãy trả về chỉ mục.

    Hướng dẫn how do you do a linear search in python? - làm cách nào để bạn thực hiện tìm kiếm tuyến tính trong python?
    Iterative Approach:

    Nếu X không phù hợp với bất kỳ yếu tố nào, hãy trả về -1.

    Ví dụ: Cách tiếp cận lặp:

    Python

    def

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    0

    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    5
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    6
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    7

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    1
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    6
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    0
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    1

    Cách tiếp cận đệ quy: & nbsp;

    Python

    def

    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    3

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    1
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    0
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    6
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    222222
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    0
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    1
    // Linear Search in C++
    
    #include <iostream>
    using namespace std;
    
    int search(int array[], int n, int x) {
    
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
    }
    1

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    9
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    6
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    0
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    1

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    1
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    0
    // Linear Search in C++
    
    #include <iostream>
    using namespace std;
    
    int search(int array[], int n, int x) {
    
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
    }
    8
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    222222

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    9
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    6
    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[].
    4

    # Linear Search in Python
    
    
    def linearSearch(array, n, x):
    
        # Going through array sequencially
        for i in range(0, n):
            if (array[i] == x):
                return i
        return -1
    
    
    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)
    if(result == -1):
        print("Element not found")
    else:
        print("Element found at index: ", result)
    1
    // Linear Search in Java
    
    class LinearSearch {
      public static int linearSearch(int array[], int x) {
      int n = array.length;
    
      // Going through array sequencially
      for (int i = 0; i < n; i++) {
        if (array[i] == x)
        return i;
      }
      return -1;
      }
    
      public static void main(String args[]) {
      int array[] = { 2, 4, 0, 1, 9 };
      int x = 1;
    
      int result = linearSearch(array, x);
    
      if (result == -1)
        System.out.print("Element not found");
      else
        System.out.print("Element found at index: " + result);
      }
    }
    6
    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[].
    7
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    0
    // Linear Search in C
    
    #include <stdio.h>
    
    int search(int array[], int n, int x) {
      
      // Going through array sequencially
      for (int i = 0; i < n; i++)
        if (array[i] == x)
          return i;
      return -1;
    }
    
    int main() {
      int array[] = {2, 4, 0, 1, 9};
      int x = 1;
      int n = sizeof(array) / sizeof(array[0]);
    
      int result = search(array, n, x);
    
      (result == -1) ? printf("Element not found") : printf("Element found at index: %d", result);
    }
    1k = 10

    Độ phức tạp thời gian của thuật toán trên là o (n). & Nbsp;

    Không gian phụ trợ: O (1) cho lặp và O (N) cho đệ quy.O(1) for iterative and O(n) for recursive.

    Vui lòng tham khảo hoàn thành bài viết về tìm kiếm tuyến tính và sự khác biệt giữa các thuật toán đệ quy và lặp lại để biết thêm chi tiết!


    Làm thế nào để bạn thực hiện tìm kiếm tuyến tính?

    Tìm kiếm tuyến tính..
    Tìm ra độ dài của tập dữ liệu ..
    Đặt bộ đếm thành 0 ..
    Kiểm tra giá trị được giữ trong danh sách tại vị trí truy cập ..
    Kiểm tra xem giá trị ở vị trí đó có phù hợp với giá trị được tìm kiếm không ..
    Nếu nó phù hợp, giá trị được tìm thấy ..

    Làm thế nào để bạn viết một thuật toán tìm kiếm tuyến tính từng bước?

    Algorithm..
    Bước 1: Chọn phần tử đầu tiên làm phần tử hiện tại ..
    Bước 2: So sánh phần tử hiện tại với phần tử đích. ....
    Bước 3: Nếu có phần tử tiếp theo, thì hãy đặt phần tử hiện tại thành phần tử tiếp theo và chuyển sang bước 2 ..
    Bước 4: Không tìm thấy phần tử đích. ....
    Bước 5: Phần tử đích tìm thấy và trở lại vị trí ..

    Tìm kiếm tuyến tính với ví dụ là gì?

    Ví dụ về thuật toán tìm kiếm tuyến tính Xem xét một mảng có kích thước 7 với các phần tử 13, 9, 21, 15, 39, 19 và 27 bắt đầu bằng 0 và kết thúc với kích thước trừ một, 6. Phần tử tìm kiếm = 39. Bước 1: Tìm kiếmPhần tử 39 được so sánh với phần tử đầu tiên của một mảng, là 13.Consider an array of size 7 with elements 13, 9, 21, 15, 39, 19, and 27 that starts with 0 and ends with size minus one, 6. Search element = 39. Step 1: The searched element 39 is compared to the first element of an array, which is 13.

    Trường hợp tốt nhất cho tìm kiếm tuyến tính trong Python là gì?

    Đối với một danh sách chứa N các mục, trường hợp tốt nhất cho tìm kiếm tuyến tính là khi giá trị đích bằng với phần tử đầu tiên của danh sách.Trong những trường hợp như vậy, chỉ cần một so sánh là cần thiết.Do đó, hiệu suất trường hợp tốt nhất là O (1).when the target value is equal to the first element of the list. In such cases, only one comparison is needed. Therefore, the best case performance is O(1).