Hướng dẫn binary search in python - tìm kiếm nhị phân trong python

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

Nội dung chính ShowShow

  • Làm cách nào để tạo một tìm kiếm nhị phân trong Python?
  • Tìm kiếm nhị phân trong Python với ví dụ là gì?
  • Làm thế nào để bạn viết một mã tìm kiếm nhị phân?
  • Có chức năng tìm kiếm nhị phân trong Python không?

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    1. Bàn luận
    2. Tóm lại, thuật toán tìm kiếm này tận dụng một bộ sưu tập các yếu tố đã được sắp xếp bằng cách bỏ qua một nửa các yếu tố chỉ sau một so sánh. & NBSP;
    3. So sánh X với phần tử giữa.
    4. Nếu X khớp với phần tử giữa, chúng ta sẽ trả về chỉ số giữa.

    Khác nếu x lớn hơn phần tử giữa, thì x chỉ có thể nằm ở nửa bên phải [lớn hơn] sau phần tử giữa. Sau đó, chúng tôi áp dụng thuật toán một lần nữa cho nửa đúng.

    Python3

    Khác nếu X nhỏ hơn, mục tiêu X phải nằm ở một nửa bên trái [thấp hơn]. Vì vậy, chúng tôi áp dụng thuật toán cho nửa trái.

    Đệ quy:

    Element is present at index 3
    3
    Element is present at index 3
    4

    Element is present at index 3
    5
    Element is present at index 3
    6
    Element is present at index 3
    7
    Element is present at index 3
    8
    Element is present at index 3
    9

    Element is present at index 3
    0__

    Element is present at index 3
    34
    Element is present at index 3
    35
    Element is present at index 3
    36

    Element is present at index 3
    3
    Element is present at index 3
    47
    Element is present at index 3
    48

    Element is present at index 3
    3
    Element is present at index 3
    38
    Element is present at index 3
    39

    Element is present at index 3
    5
    Element is present at index 3
    47
    Element is present at index 3
    48

    Element is present at index 3
    34
    Element is present at index 3
    35
    Element is present at index 3
    42
    Element is present at index 3
    43
    Element is present at index 3
    44
    Element is present at index 3
    45

    Element is present at index 3
    34
    Element is present at index 3
    35
    Element is present at index 3
    51

    Element is present at index 3
    3
    Element is present at index 3
    44
    Element is present at index 3
    54

    Element is present at index 3
    3
    Element is present at index 3
    35
    Element is present at index 3
    43
    Element is present at index 3
    44

    Element is present at index 3
    62
    Element is present at index 3
    8
    Element is present at index 3
    64

    Element is present at index 3
    7
    Element is present at index 3
    66
    Element is present at index 3
    67____56
    Element is present at index 3
    69
    Element is present at index 3
    66
    Element is present at index 3
    71
    Element is present at index 3
    66
    Element is present at index 3
    73
    Element is present at index 3
    74

    Element is present at index 3
    75
    Element is present at index 3
    8
    Element is present at index 3
    71

    Element is present at index 3
    5
    Element is present at index 3
    95
    Element is present at index 3
    96
    Element is present at index 3
    97
    Element is present at index 3
    66
    Element is present at index 3
    99
    Element is present at index 3
    30

    Element is present at index 3
    47
    Element is present at index 3
    48

    Element is present at index 3
    5
    Element is present at index 3
    95
    Element is present at index 3
    96
    Element is present at index 3
    36
    Element is present at index 3
    37

    Output:      

    Element is present at index 3
    78
    Element is present at index 3
    8
    Element is present at index 3
    80
    Element is present at index 3
    81
    Element is present at index 3
    66
    Element is present at index 3
    83
    Element is present at index 3
    84
    Element is present at index 3
    43
    Element is present at index 3
    44
    Element is present at index 3
    45

    Element is present at index 3
    6
    Element is present at index 3
    89
    Element is present at index 3
    8
    Element is present at index 3
    43
    Element is present at index 3
    44
    Element is present at index 3
    48: O[log n]
    : O[log n]

    Element is present at index 3
    : O[logn]     [NOTE: Recursion creates Call Stack]

    Iterative:  
     

    Python3

    Độ phức tạp về thời gian: O [log n]

    Không gian phụ trợ: O [logn] & nbsp; & nbsp; [Lưu ý: đệ quy tạo ra ngăn xếp cuộc gọi]

    Element is present at index 3
    3
    Element is present at index 3
    39

    Element is present at index 3
    01
    Element is present at index 3
    8
    Element is present at index 3
    81
    Element is present at index 3
    05
    Element is present at index 3
    8
    Element is present at index 3
    83
    Element is present at index 3
    08
    Element is present at index 3
    43
    Element is present at index 3
    44

    Element is present at index 3
    3
    Element is present at index 3
    4

    Element is present at index 3
    5
    Element is present at index 3
    6
    Element is present at index 3
    7
    Element is present at index 3
    8
    Element is present at index 3
    9

    Element is present at index 3
    34
    Element is present at index 3
    35
    Element is present at index 3
    36

    Element is present at index 3
    3
    Element is present at index 3
    47
    Element is present at index 3
    48

    Element is present at index 3
    0__

    Element is present at index 3
    34
    Element is present at index 3
    35
    Element is present at index 3
    51

    Element is present at index 3
    3
    Element is present at index 3
    44
    Element is present at index 3
    54

    Element is present at index 3
    3
    Element is present at index 3
    35
    Element is present at index 3
    43
    Element is present at index 3
    44

    Element is present at index 3
    62
    Element is present at index 3
    8
    Element is present at index 3
    64

    Element is present at index 3
    7
    Element is present at index 3
    66
    Element is present at index 3
    67____56
    Element is present at index 3
    69
    Element is present at index 3
    66
    Element is present at index 3
    71
    Element is present at index 3
    66
    Element is present at index 3
    73
    Element is present at index 3
    74

    Element is present at index 3
    75
    Element is present at index 3
    8
    Element is present at index 3
    71

    Element is present at index 3
    5
    Element is present at index 3
    95
    Element is present at index 3
    96
    Element is present at index 3
    97
    Element is present at index 3
    66
    Element is present at index 3
    99
    Element is present at index 3
    30

    Element is present at index 3
    47
    Element is present at index 3
    48

    Element is present at index 3
    5
    Element is present at index 3
    95
    Element is present at index 3
    96
    Element is present at index 3
    36
    Element is present at index 3
    37

    Output:      

    Element is present at index 3

    Element is present at index 3
    6
    Element is present at index 3
    89
    Element is present at index 3
    8
    Element is present at index 3
    43
    Element is present at index 3
    44
    Element is present at index 3
    48: O[log n]
    : O[log n]

    Element is present at index 3
    : O[logn]     [NOTE: Recursion creates Call Stack]
    Please refer to the article Binary Search for more details!
     


    Làm cách nào để tạo một tìm kiếm nhị phân trong Python?

    Algorithm...

    Độ phức tạp về thời gian: O [log n]

    Không gian phụ trợ: O [logn] & nbsp; & nbsp; [Lưu ý: đệ quy tạo ra ngăn xếp cuộc gọi]

    Element is present at index 3
    3
    Element is present at index 3
    39

    Element is present at index 3
    01
    Element is present at index 3
    8
    Element is present at index 3
    81
    Element is present at index 3
    05
    Element is present at index 3
    8
    Element is present at index 3
    83
    Element is present at index 3
    08
    Element is present at index 3
    43
    Element is present at index 3
    44

    Tìm kiếm nhị phân trong Python với ví dụ là gì?

    Element is present at index 3
    0__ a searching algorithm which is used to search an element from a sorted array. It cannot be used to search from an unsorted array. Binary search is an efficient algorithm and is better than linear search in terms of time complexity. The time complexity of linear search is O[n].

    Làm thế nào để bạn viết một mã tìm kiếm nhị phân?

    Element is present at index 3
    : O[1]Please refer to the article Binary Search for more details! .

    So sánh x với phần tử giữa ..

    Nếu x khớp với phần tử giữa, chúng ta sẽ trả về chỉ số giữa ..

    Khác nếu x lớn hơn phần tử giữa, thì x chỉ có thể nằm ở nửa bên phải Subarray sau phần tử giữa. Vì vậy, chúng tôi tái diễn cho nửa đúng ..

    Khác [x nhỏ hơn] tái diễn cho nửa bên trái ..

    Tìm kiếm nhị phân là một thuật toán tìm kiếm được sử dụng để tìm kiếm một phần tử từ một mảng được sắp xếp.Nó không thể được sử dụng để tìm kiếm từ một mảng chưa được phân loại.Tìm kiếm nhị phân là một thuật toán hiệu quả và tốt hơn so với tìm kiếm tuyến tính về độ phức tạp về thời gian.Độ phức tạp thời gian của tìm kiếm tuyến tính là O [N].a searching algorithm which is used to search an element from a sorted array. It cannot be used to search from an unsorted array. Binary search is an efficient algorithm and is better than linear search in terms of time complexity. The time complexity of linear search is O[n].

    Ví dụ tìm kiếm nhị phân trong Java..

    lớp nhị phân nghiên cứu {.

    công khai static void BinarySearch [int arr [], int First, int cuối cùng, int key] {.

    Có chức năng tìm kiếm nhị phân trong Python không?

    int mid = [đầu tiên + cuối cùng]/2 ;.. It divides a list in half. If a specified value is higher than the middle number, the search focuses on the right of the list. Otherwise, the search looks for the number on the left of the list.

    Bài Viết Liên Quan

    Chủ Đề