WOLFRAM|DEMONSTRATIONS PROJECT

Time Complexity of Common Sorting Algorithms

​
scale
linear
logarithmic
plot range
fixed
automatic
number of elements
10
O(
2
n
)
bubble
selection
insertion
O(n log(n))
merge
quick
heap
O(n n!)
bogo
Plot[{
2
(If[MemberQ[showsort$75420,1],#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
,
2
(If[MemberQ[showsort$75420,2],#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
,
2
(If[MemberQ[showsort$75420,3],#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
,(If[MemberQ[showsort$75420,
4],#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
Log[FE`elements$$749843862061055506760323789686350020969],(If[MemberQ[showsort$75420,5],
#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
Log[FE`elements$$749843862061055506760323789686350020969],(If[MemberQ[showsort$75420,6],
#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
Log[FE`elements$$749843862061055506760323789686350020969],(If[MemberQ[showsort$75420,7],
#1,{}]&)[FE`elements$$749843862061055506760323789686350020969]
FE`elements$$749843862061055506760323789686350020969!},{FE`elements$$749843862061055506760323789686350020969,1,
FE`elements$$749843862061055506760323789686350020969},ImageSize{500,300},PlotLabeltime complexity,AxesLabel{number
of elements,operations},AxesOrigin{1,0},PlotRangeWhich[FE`chooseplot$$749843862061055506760323789686350020969===
Plot&&FE`fixaxes$$7498438620610555067603237896863500209691,{{0,20},{0,500}},
FE`chooseplot$$749843862061055506760323789686350020969===LogPlot&&FE`fixaxes$$749843862061055506760323789686350020969
1,{{1,20},{1,10000}},FE`fixaxes$$7498438620610555067603237896863500209690,Automatic],PlotLegendsPlaced[{bubble ,
selection ,insertion ,merge ,quick ,heap ,bogo },Below],PlotStyle{{Darker[Green],
Thickness[0.015]},{Orange,Dashed,Thickness[0.01]},Red,{Brown,Thickness[0.015]},{Darker[Yellow],Dashed,
Thickness[0.01]},Blue,Normal}]
This Demonstration shows the average Big-O complexity of some common sorting algorithms as the number of elements in the unsorted list increases. Most sorting algorithms run in
O(
2
n
)
or
O(nlog(n))
time. Bogosort (randomize the unsorted list and check if it is sorted) is included for comparison.