home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!agate!peoplesparc.Berkeley.EDU!fateman
- From: fateman@peoplesparc.Berkeley.EDU (Richard Fateman)
- Newsgroups: sci.math.symbolic
- Subject: Quicksort in Mathematica
- Date: 23 Nov 1992 18:44:44 GMT
- Organization: University of California, Berkeley
- Lines: 94
- Message-ID: <1er8qsINN7cj@agate.berkeley.edu>
- NNTP-Posting-Host: peoplesparc.berkeley.edu
-
-
- Someone asked me why I said earlier in this thread why an O(n log n)
- algorithm might be O(n^3) in Mathematica.
-
- (This would be particularly embarrassing if it were being
- used in a CS algorithms course, but similar concerns might
- surface if one were to compare various RK formulas in a
- computational physics class.)
-
- I tried out this simple program, and it suggests that quicksort
- in Mathematica is not O(n^3). But it is not O(n log n) either.
-
- I've spent too much time on this example this morning already,
- I would be interested in seeing the results of a closer look, from
- someone who is considering using Mathematica for CS students.
- RJF.
-
-
- (* Here's a quicksort written in mathematica *)
-
- sort[l_Integer,r_Integer] := Block[{i,j,x,w},
- i=l; j=r;
- x= A[[Floor[(l+r)/2]]];
- For[, i<=j,,
- While [A[[i]] < x, i++];
- While [x < A[[j]], j--];
- If [i<=j,
- Block[{}, w=A[[i]];A[[i]]=A[[j]];A[[j]]=w; i++; j--]]];
- If [l<r,sort[l,j]];
- If [i<r,sort[i,r]]]
-
- (*yes, that's all there is to quicksort if you wish to sort just one
- array A of n numbers, and call the sort routine by sort[1,n]. *)
-
- (*Here are some programs to test it *)
-
- makeA[n_]:= A=Table[Random[Integer,100],{i,1,n}]
-
- testsort[n_]:=Block[{},makeA[n];sort[1,n]]
-
- (* results on MIPS-120 with Mathematica 2.0 for Timing[Do[testsort[n],{2}]] *)
-
- (*note.. the built-in fn takes 0.15 sec for sorting 1000 numbers, 0.37 for
- sorting 4000 elements.
- (* above 3000, we timed just 1 run, not averaged, times 2. *)
-
- results={{25,0.452},
- {50,0.994},
- {75, 1.61},
- {100,2.23},
- {200,5.17},
- {300,9.37},
- {400,12.23},
- {500,17.16},
- {800,33.97},
- {1000,47.51},
- {2000,149.95},
- {3000,326.28},
- {4000,561.7}}
-
- p1=ListPlot[results,PlotJoined->True, PlotRange->All]
- (* compare mathematica to n*log(n), match at n=300 *)
- p2=Plot[0.005476*n*Log[n],{n,1,4000}]
- Show[{p1,p2}]
-
- (* compare mathematica to n^2, match at n=1000 *)
- p3=Plot[0.00006864*n^2,{n,1,4000}]
- Show[{p1,p3}]
-
- (* See all three -- *)
-
- Show[{p1,p2,p3}]
-
- (*Conclusions: The quicksort program above, as
- executed by Mathematica, does not run in time proportional
- to not n log n. It is much slower.
-
- It is not as slow as n^2, at least in the range 25 to 4000.
-
- It is ridiculously slow compared to the built-in Sort, for any size.
-
- On the positive side, Mathematica's graphing tools are handy to show
- how inefficient its programming language is. *)
-
-
-
-
-
-
-
-
- --
- Richard J. Fateman
- fateman@cs.berkeley.edu 510 642-1879
-