Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1.  
  2. unsigned long binsearch(unsigned long long int nel, int (*less)(unsigned long i,unsigned long j))
  3. {
  4. unsigned long long int low = 0, high = nel-1, mid;
  5. while ( low <= high)
  6. {
  7. mid = low/2+high/2;
  8. if (less(high-1,high)&&less(high+1,high))
  9. return high;
  10. if (less(low-1,low)&&less(low+1,low))
  11. return low;
  12. if (less(mid-1,mid) && less(mid+1,mid))
  13. return mid;
  14. if(less(mid-1,mid))
  15. low = mid + 1;
  16. else
  17. if (less(mid+1,mid))
  18. high = mid - 1;
  19. else
  20. low=mid +1;
  21. }
  22. return nel;
  23. }
  24.  
  25. unsigned long peak(unsigned long nel,
  26. int (*less)(unsigned long i, unsigned long j))
  27. {
  28. return binsearch(nel,less);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement