kburnik

LOGO - Silba 2012 - Zadatak: BinSearch: Kristijan Burnik

Aug 23rd, 2012
30
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. to bin.search :lista :t
  2. (local "s "e "koraci "c "el)
  3. make "l (sort :lista)
  4.  
  5. make "s 0
  6. make "e count :l
  7. make "koraci []
  8.  
  9. while [ :s < :e ] [
  10.  
  11. ;; racunamo sredinu intervala
  12. make "c int ((:s + :e)/2)
  13.  
  14. ;; "izvlacimo" sredisnji element
  15. make "el item (:c+1) :l
  16.  
  17. ;; biljezimo korak
  18. make "koraci lput :el :koraci
  19.  
  20. ;; ako je sredisnji element jednak trazenom, pretraga je gotova
  21. ifelse :el = :t [
  22. pr :koraci
  23. op "true
  24. ] [
  25. ;; u kojem podintervalu bi se element mogao nalaziti?
  26. ifelse (:el < :t) [
  27. ; s desne strane sredisnjeg elementa
  28. make "s :c
  29. ][
  30. ;; s lijeve strane sredisnjeg elementa
  31. make "e :c
  32. ]
  33.  
  34. ]
  35.  
  36. ]
  37. ;; ispisi korake, element nije pronadjen
  38. pr :koraci
  39. op "false
  40. end
  41. ;;;;;;;;;;;;;
  42. ;; primjer ;;
  43. ;;;;;;;;;;;;;
  44. make "l [1 10 3 4 7 6 8 9 2 5]
  45. repeat (count :l) [
  46. pr bin.search :l (pick :l)
  47. ]
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×