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$798732,1],#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
,
2
(If[MemberQ[showsort$798732,2],#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
,
2
(If[MemberQ[showsort$798732,3],#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
,(If[MemberQ[showsort$798732,
4],#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
Log[FE`elements$$2885430945379446316567096958866319151424],(If[MemberQ[showsort$798732,5],
#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
Log[FE`elements$$2885430945379446316567096958866319151424],(If[MemberQ[showsort$798732,6],
#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
Log[FE`elements$$2885430945379446316567096958866319151424],(If[MemberQ[showsort$798732,7],
#1,{}]&)[FE`elements$$2885430945379446316567096958866319151424]
FE`elements$$2885430945379446316567096958866319151424!},{FE`elements$$2885430945379446316567096958866319151424,1,
FE`elements$$2885430945379446316567096958866319151424},ImageSize{500,300},PlotLabeltime complexity,AxesLabel{number of
elements,operations},AxesOrigin{1,0},PlotRangeWhich[FE`chooseplot$$2885430945379446316567096958866319151424===Plot&&
FE`fixaxes$$28854309453794463165670969588663191514241,{{0,20},{0,500}},
FE`chooseplot$$2885430945379446316567096958866319151424===LogPlot&&FE`fixaxes$$2885430945379446316567096958866319151424
1,{{1,20},{1,10000}},FE`fixaxes$$2885430945379446316567096958866319151424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.