home *** CD-ROM | disk | FTP | other *** search
/ Ahoy 1987 August / Ahoy_Magazine_87-08_1987_Double_L_Side_A.d64 / Quicksort (.txt) < prev    next >
Commodore BASIC  |  2022-10-26  |  928b  |  27 lines

  1. 0 poke646,0:poke53280,12:poke53281,12
  2. 1 rem ==================================
  3. 2 rem    demonstration of quicksort by
  4. 3 rem       ivan rudyk
  5. 4 rem ==================================
  6. 10 q=180 : dim n(q),m(q)
  7. 20 print"[147]generating numbers...":for x=1 to q:n(x)=int(1000*rnd(0))+1:next x
  8. 30 print"sorting..." : t0=ti
  9. 40 m(1)=1 : m(2)=q : a=2
  10. 50 b=m(a) : a=a-1 : c=m(a) : a=a-1 : e=c
  11. 60 f=b : d=n((c+b)/2)
  12. 70 if n(e)<d then e=e+1 : goto 70
  13. 80 if n(f)>d then f=f-1 : goto 80
  14. 90 if e<=f then z=n(e) : n(e)=n(f) : n(f)=z : e=e+1 : f=f-1
  15. 100 if e<=f then 70
  16. 110 if c<f then a=a+1 : m(a)=c : a=a+1 : m(a)=f
  17. 120 c=e : if c<b then 60
  18. 130 if a<>0 then 50
  19. 140 for x=1 to q : print n(x); : next x
  20. 145 print ti-t0"jiffies" : end
  21. 200 rem ================================
  22. 210 rem     conventional bubble sort
  23. 220 rem      (add line 35 goto 240)
  24. 230 rem ================================
  25. 240 for j=1 to q-1 :for k=j+1 to q : if n(j)>n(k) then t=n(j) :n(j)=n(k) :n(k)=t
  26. 250 next k,j : for x=1 to q : print n(x); : next x : print ti-t0"jiffies" : end
  27.