Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. def binarysearch(A,start,end,x):
  2. if (start > end):
  3. return -1
  4. mid=(start+end)//2
  5. if (A[mid]==x):
  6. return mid
  7. else:
  8. if(x< A[mid]):
  9. return binarysearch(A,start,mid-1,x)
  10. else:
  11. return binarysearch(A,mid+1,end,x)
  12.  
  13. def findCount(A,x):
  14. n=len(A)
  15. start=0
  16. end=n-1
  17.  
  18. index= binarysearch(A,start,end,x)
  19. if (index==-1):
  20. return 0
  21.  
  22. count=1
  23. left=index-1
  24. return index
  25. A=[5,5,6,6,6,6,7,8,8,8,9,9,9]
  26. x=9
  27. print(findCount(A,x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement