[Python] ์ด๋ถ„ ํƒ์ƒ‰

2020. 8. 26. 00:07ยทAlgorithm/Python

์ด๋ถ„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

def binary_search(a, x):
    start = 0
    end = len(a) - 1

    while start <= end:
        mid = (start + end) // 2
        if x == a[mid]:
            return mid
        elif x > a[mid]:
            start = mid + 1
        else:
            end = mid - 1

    return -1

d = [1, 4, 9, 16, 25, 36, 49, 64, 81]
print(binary_search(d, 36))
print(binary_search(d, 50))

#1 ์ค‘๊ฐ„ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค.

#2 ์ฐพ๋Š” ๊ฐ’๊ณผ ์ค‘๊ฐ„ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.

#3 ๊ฐ™๋‹ค๋ฉด ์œ„์น˜๋ฒˆํ˜ธ๋ฅผ ๋Œ๋ ค์ฃผ๊ณ 

#4 ์ฐพ๋Š” ๊ฐ’์ด ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์˜ ์˜ค๋ฅธ์ชฝ์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰(1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ)

#5 ์ฐพ๋Š” ๊ฐ’์ด ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์˜ ์™ผ์ชฝ์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰(1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ)

 

 

 

'Algorithm > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ฐพ๊ธฐ  (1) 2020.09.07
[Python] ํ์™€ ์Šคํƒ(queue, stack)  (0) 2020.08.31
[Python] ํ€ต ์ •๋ ฌ  (0) 2020.08.25
[Python] ๋ณ‘ํ•ฉ์ •๋ ฌ  (0) 2020.08.23
[Python] ์‚ฝ์ž…์ •๋ ฌ  (0) 2020.08.22
'Algorithm/Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Python] ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ฐพ๊ธฐ
  • [Python] ํ์™€ ์Šคํƒ(queue, stack)
  • [Python] ํ€ต ์ •๋ ฌ
  • [Python] ๋ณ‘ํ•ฉ์ •๋ ฌ
๐ŸฅญMango
๐ŸฅญMango
  • ๐ŸฅญMango
    AppleMango๐Ÿฅญ
    ๐ŸฅญMango
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ
      • iOS
        • Swift
        • SwiftUI
        • RxSwift
      • Algorithm
        • C
        • Python
        • Swift
      • Computer Science
        • ์ปดํ“จํ„ฐ ๋„คํŠธ์›Œํฌ
        • OS
      • ...
      • ๊ฐœ๋ฐœ ํƒ€์ž„์บก์А
        • Python
        • Flutter
        • Android
        • Kotlin
        • Java
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    1613 ํŒŒ์ด์ฌ
    14503 ํŒŒ์ด์ฌ
    typing animation
    swiftUI Gradients
    Apple Login
    SwiftUI Apple Login
    6118
    IOS
    ํŒŒ์ด์ฌ ํ† ๋งˆํ† 
    ํŒŒ์ด์ฌ 1459
    ๋ฐฑ์ค€ ์†Œ์ˆ˜
    ํŒŒ์ด์ฌ 14503
    1613 ์—ญ์‚ฌ
    ํŒŒ์ด์ฌ 6118
    ํŒŒ์ด์ฌ ์ •๋ ฌ
    14503 ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ
    ํ† ๋งˆํ† 
    SwiftUI
    ์ฝ”ํ‹€๋ฆฐ ์ƒ์†
    MapMarker
    Custom Map Marker
    Swift Hello World!
    Code Base
    ์ฝ”ํ‹€๋ฆฐ
    ์Šคํƒ
    1์ฐจ์› ๋ฟŒ์š”๋ฟŒ์š”
    ๋ฐฑ์ค€ ํ† ๋งˆํ† 
    ํŒŒ์ด์ฌ
    swiftUI tabview
    ์—ญ์‚ฌ ํŒŒ์ด์ฌ
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.2
๐ŸฅญMango
[Python] ์ด๋ถ„ ํƒ์ƒ‰
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”