conversations
list
source
string
stack
string
[ { "from": "human", "value": "Add Wolfram Language implementation of bubble sort\n" }, { "from": "gpt", "value": "#!/usr/bin/env -S wolfram -script\n\n(*\n\tbubblesort takes a list as argument and repeatedly applies a substitution \n\tuntil the pattern can't be matched anymore.\n\t\n\tThe pattern '{left___, x_, y_, right___} /; y < x' matches any list where\n\ttwo elements x and y with y < x exist. \n\t'left___' and 'right___' are BlanckNullSequences, i.e. sequences with\n\tvariable length (zero length sequences allowed).\n\tThe above pattern then gets replaced with the similar pattern\n\t'{left, y, x, right}', where y and x are switched.\n\n\tNote: ReplaceRepeated[pattern][list] can be written as list .// pattern in\n\t short form.\n*)\n\nbubblesort[list_] := ReplaceRepeated[\n\t\t{left___, x_, y_, right___} /; y < x -> {left, y, x, right}\n\t][list];\n\n(*Example list*) \nlist = {1, 52, 55, 100, 100, 26}\nPrint[bubblesort[list]]\n" } ]
mathematica
chosen