Perl, 62 bytes
Includes +3 for -p
Give input as a single line of numbers on STDIN:
perl -M5.010 visisort.pl <<< "3 0 4 2 1"
Repeatedly swaps the first inversion. Swap complexity is O(n^2)
, time complexity is O(n^3)
. Uses the numbers being swapped as mark:
3 0 4 2 1
3 0
0 3 4 2 1
4 2
0 3 2 4 1
3 2
0 2 3 4 1
4 1
0 2 3 1 4
3 1
0 2 1 3 4
2 1
0 1 2 3 4
visisort.pl
:
#!/usr/bin/perl -p
$&>$'&&say$_.$"x"@-".!s/(\S+) \G(\S+)/$2 $1/.$&while/\S+ /g
The program also supports negative values and floating point numbers
If you insist on a connecting character the code becomes 6566 bytes:
#!/usr/bin/perl -p
$&>$'&&say$_.$"x"@-".!s/(\S+) \G(\S+)/$2 $1/.$1.-$2while/\S+ /g
But now it doesn't support negative numbers and 0 anymore (but the program only has to support positive integers anyways. The 0
in the example is a mistake)