Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
m (→‎{{header|Pascal}}: indentation)
Line 1,485: Line 1,485:


=={{header|Mathematica}}==
=={{header|Mathematica}}==
<lang Mathematica>bubbleSort[{w___, x_, y_, z___}] /; x > y := bubbleSort[{w, y, x, z}]
A rule-based solution is only one line, for large lists this method is not optimal, not so because of the method but because of the usage of patterns in a rule based solution:
bubbleSort[sortedList_] := sortedList</lang>
<lang Mathematica>BubbleSort[input_] := input //. {a___, i_, j_, b___} /; OrderedQ[{j, i}] :> {a, j, i, b}</lang>
Example:
Example:
<lang Mathematica>BubbleSort[{10, 3, 7, 1, 4, 3, 8, 13, 9}]</lang>
<pre>bubbleSort[{10, 3, 7, 1, 4, 3, 8, 13, 9}]
{1, 3, 3, 4, 7, 8, 9, 10, 13}</pre>
gives back:
<lang Mathematica>{1, 3, 3, 4, 7, 8, 9, 10, 13}</lang>


=={{header|MATLAB}}==
=={{header|MATLAB}}==