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: |
||
< |
<pre>bubbleSort[{10, 3, 7, 1, 4, 3, 8, 13, 9}] |
||
⚫ | |||
gives back: |
|||
⚫ | |||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |