728x90
반응형
수행 코드
dataSourceAligned = [15,20,29,30,37, 55, 69, 73, 87, 90]
def my_index( toSearchValue, dataSource ):
sizeData = len( dataSource )
low = 0
high = sizeData-1
nTrials = 0
while( low <= high ):
middle = int((low+high)/2)
nTrials += 1
#print(" Trial :" , nTrials, "middle : ", middle )
if( dataSource[middle] == toSearchValue ):
#print("탐색 성공 at" , middle, "index" )
break
elif( toSearchValue >= dataSource[middle] ):
low = middle + 1
else:
high = middle - 1
if( low<= high):
return middle
else:
return -1
toSearchValue = 30
index = my_index( toSearchValue, dataSourceAligned)
if( index >=0 ):
print("Found", toSearchValue, "value", "at", index, "index" )
else:
print("NOT Found", toSearchValue, "value" )
수행결과 화면
PS D:\PythonCode> python .\BinarySearch.py
Found 30 value at 3 index
PS D:\PythonCode>
728x90
반응형
'프로그램언어(JAVA , Python)' 카테고리의 다른 글
[Python] 삽입정렬(Insertion Sorting) (0) | 2023.11.30 |
---|---|
[Python] 버블정렬(Bubble Sorting) (0) | 2023.11.30 |
[Python] 선형탐색( Linear Search) (0) | 2023.11.30 |
[Python] tkinter를 활용한 구구단 게임. (0) | 2023.11.29 |
웹 통신 프로토콜... (0) | 2023.11.27 |