fork download
  1. data_t=[40,60,20,88,44,22] ; n=len(data_t)
  2. data_t.sort(reverse=True)
  3. print("探索値は?")
  4. num = int(input())
  5. left = 0
  6. right = n-1
  7. while left <= right:
  8. c = (left+right) // 2
  9. if data_t[c] == num:
  10. print(f"{num}は左から{c+1}番目に存在")
  11. break
  12. elif data_t[c] > num:
  13. left = c + 1
  14. else:
  15. right = c - 1
  16. print(data_t)
  17.  
Success #stdin #stdout 0.03s 9788KB
stdin
88
stdout
探索値は?
88は左から1番目に存在
[88, 60, 44, 40, 22, 20]