Python의 선형 검색

  1. 선형 검색 알고리즘
  2. 선형 검색 Python 구현
Python의 선형 검색
노트
Linear Search에 대한 자세한 내용은 순차 검색 알고리즘 문서를 참조하세요.

선형 검색 알고리즘

n요소를 포함하는 정렬되지 않은 배열A[]가 있다고 가정하고,X요소를 찾으려고합니다.

선형 검색 Python 구현

Python
 pythonCopydef linearsearch(arr, n, x):

    for i in range(0, n):
        if arr[i] == x:
            return i
    return -1


arr = [1, 2, 3, 4, 5]
x = 1
n = len(arr)
position = linearsearch(arr, n, x)
if position == -1:
    print("Element not found !!!")
else:
    print("Element is present at index", position)

출력:

 textCopyElement is found at index: 1

위 알고리즘의 시간 복잡도는O(n)입니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
Harshit Jindal avatar Harshit Jindal avatar

Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.

LinkedIn

관련 문장 - Python Algorithm