Visualize sorting
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 66 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)
PHP, 248 Bytes
Bubblesort boring wins
<?for($c=count($a=$_GET[a]);$c--;){for($s=$i=0;$i<$c;){$l=strlen($j=join(",",$a));if($a[$i]>$a[$i+1]){$t=$a[$i];$a[$i]=$a[$i+1];$a[$i+1]=$t;$m=" ";$m[$s]=I;$m[$s+strlen($a[$i].$a[$i+1])]=X;echo"$j\n$m\n";}$s+=strlen($a[$i++])+1;}}echo join(",",$a);
PHP, 266 Bytes a way with array_slice and min
modified output I X
instead of *~~*
<?for($c=count($a=$_GET[a]);$i<$c;){$j=join(",",$s=($d=array_slice)($a,$i));$x=array_search($m=min($s),$s);echo($o=join(",",$a));$a[$x+$i]=$a[$i];$a[$i]=$m;if($i++!=$c-1){$t=" ";$t[$z=($f=strlen)($o)-($l=$f($j))]=I;$t[$l+$z-$f(join(",",$d($s,$x)))]=X;echo"\n$t\n";}}
282 Bytes
<?for($c=count($a=$_GET[a]);$i<$c;){$j=join(",",$s=($d=array_slice)($a,$i));$x=array_search($m=min($s),$s);echo($o=join(",",$a));$a[$x+$i]=$a[$i];$a[$i]=$m;if($i++!=$c-1)echo"\n".str_repeat(" ",($f=strlen)($o)-($l=$f($j))).($x?str_pad("*",$l-$f(join(",",$d($s,$x))),"~"):"")."*\n";}
How it works
Looks for the minimum in an array and take this on first position Look for the minimum without first position .... and so on If a value is double the first value will be swap
Output Example
31,7,0,5,5,5,753,5,99,4,333,5,2,1001,35,1,67
*~~~~*
0,7,31,5,5,5,753,5,99,4,333,5,2,1001,35,1,67
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
0,1,31,5,5,5,753,5,99,4,333,5,2,1001,35,7,67
*~~~~~~~~~~~~~~~~~~~~~~~~~*
0,1,2,5,5,5,753,5,99,4,333,5,31,1001,35,7,67
*~~~~~~~~~~~~~~*
0,1,2,4,5,5,753,5,99,5,333,5,31,1001,35,7,67
*
0,1,2,4,5,5,753,5,99,5,333,5,31,1001,35,7,67
*
0,1,2,4,5,5,753,5,99,5,333,5,31,1001,35,7,67
*~~~*
0,1,2,4,5,5,5,753,99,5,333,5,31,1001,35,7,67
*~~~~~~*
0,1,2,4,5,5,5,5,99,753,333,5,31,1001,35,7,67
*~~~~~~~~~~*
0,1,2,4,5,5,5,5,5,753,333,99,31,1001,35,7,67
*~~~~~~~~~~~~~~~~~~~~~*
0,1,2,4,5,5,5,5,5,7,333,99,31,1001,35,753,67
*~~~~~~*
0,1,2,4,5,5,5,5,5,7,31,99,333,1001,35,753,67
*~~~~~~~~~~~*
0,1,2,4,5,5,5,5,5,7,31,35,333,1001,99,753,67
*~~~~~~~~~~~~~~~*
0,1,2,4,5,5,5,5,5,7,31,35,67,1001,99,753,333
*~~~~*
0,1,2,4,5,5,5,5,5,7,31,35,67,99,1001,753,333
*~~~~~~~~*
0,1,2,4,5,5,5,5,5,7,31,35,67,99,333,753,1001
*
0,1,2,4,5,5,5,5,5,7,31,35,67,99,333,753,1001
JavaScript (ES6), 158 bytes
a=>{for(;;){console.log(``+a);i=a.findIndex((e,i)=>e<a[i-1]);if(i<0)break;console.log(` `.repeat(`${a.slice(0,i)}`.length-1)+`|-|`);t=a[i];a[i]=a[--i];a[i]=t}}
Bubble sort. Sample output:
3,0,4,2,1
|-|
0,3,4,2,1
|-|
0,3,2,4,1
|-|
0,2,3,4,1
|-|
0,2,3,1,4
|-|
0,2,1,3,4
|-|
0,1,2,3,4