(* any code herein that is not covered by license terms as indicated or as stated on the source website is hereby released into the public domain *)
nTrials[sortFunc_,n_]:=({#[[1]],Round[#[[2]]/n 100,2]}&/@#)&/@Sort/@Tally/@Transpose[Table[sortFunc[{0,1,2,3,4},(RandomInteger[1]==1)&],{t,1,n}]];millionTrials[sortFunc_]:=nTrials[sortFunc,1000000];(*--------------------------------------------------------------*)
(* from http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#Mathematica (GNU FDL), modified to take arbitrary comparison function *)BubbleSort[input_,fn_]:=input//.{a___,i_,j_,b___}/;fn[j,i]:>{a,j,i,b};
(*--------------------------------------------------------------*)
(*Copyright (c) 2010 the authors listed at the following URL,and/or the authors of referenced articles or incorporated external code:http://en.literateprograms.org/Quicksort_(Mathematica)?action=history&offset=20060512235643 Permission is hereby granted,free of charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Retrieved from:http://en.literateprograms.org/Quicksort_(Mathematica)?oldid=4625, modified to take arbitrary comparison function *)QuickSort[list_, fn_]:=Block[{list2,pivot,pivotidx},If[Length[list]>1,(pivotidx=Random[Integer,{1,Length[list]}];
pivot=list[[pivotidx]];
list2=Delete[list,pivotidx];Block[{left=Select[list2,fn[#,pivot]&]},
Join[QuickSort[left,fn],List[pivot],QuickSort[Complement[list2,left],fn]]]),(*else*)list]];(*--------------------------------------------------------------*)(* based on code in http://books.google.com/books?id=OuzV1M7b0WIC&pg=PA198&lpg=PA198&dq=mathematica+mergesort&source=bl&ots=HB3j-gHceJ&sig=AAR1abNZMDL9OmWrN6jutM18sZQ&hl=en&ei=GX2FS8q3CIqmNubjyTQ&sa=X&oi=book_result&ct=result&resnum=7&ved=0CCIQ6AEwBg#v=onepage&q=mathematica%20mergesort&f=false *)
mergeList[lis_List,{},fn_]:=lis;mergeList[{},lis_List,fn_]:=lis;mergeList[{a_,ra___},{b_,rb___},fn_]:=If[fn[a,b],Join[{a},mergeList[{ra},{b,rb},fn]],Join[{b},mergeList[{a,ra},{rb},fn]]];MergeSort[{},fn_]:={};MergeSort[{x_},fn_]:={x};MergeSort[lis_List,fn_]:=Module[{div=Floor[Length[lis]/2]},mergeList[MergeSort[Take[lis,div],fn],MergeSort[Drop[lis,div],fn],fn]];
(*--------------------------------------------------------------*)
(* Combinatorica provides SelectionSort and HeapSort, but HeapSort doesn't take a comparison function *)<<Combinatorica`;
(*--------------------------------------------------------------*)(* NOTE: BubbleSort fails when there are duplicate elements; QuickSort may remove duplicate elements at random *)

testList=RandomInteger[100,10];Table[{fn,fn[testList,OrderedQ[{#1,#2}]&]},{fn,{Sort,BubbleSort,QuickSort,MergeSort,SelectionSort}}]

TableForm[{#[[1]],Grid[Transpose[Join[{{"","first","second","third","fourth","fifth"}},Transpose[Join[{{0,1,2,3,4}},((#[[2]]&/@#)&/@#[[2]])]]]],Frame->All]}&/@ParallelTable[{fn,millionTrials[fn]},{fn,{Sort,BubbleSort,QuickSort,MergeSort,SelectionSort}}]]