resource sorter() procedure print_array(ref a[1:*] : int) const MAX_PER_LINE : int := 15 var count : int := 0 printf("comment ") # for animator fa i := 1 to ub(a) -> writes(" ", a[i]); count++ if count mod MAX_PER_LINE = 0 -> writes("\n") if i < ub(a) -> printf("comment ") /* for animator */ fi fi af if count mod MAX_PER_LINE != 0 -> writes("\n") fi end print_array var N : int := 10; getarg(1, N) var RANGE : int := 100; getarg(2, RANGE) printf("comment ") # for animator write("Bubble sorting", N, "numbers between 1 and", RANGE) printf("comment ") # for animator write(" (or read POSITIVE numbers from command line)") ##### animator #####v procedure scale_x(x : int) returns x_scaled : real x_scaled := real(x)/N end scale_x procedure scale_y(y : int) returns y_scaled : real y_scaled := real(y)/RANGE end scale_y ##### animator #####^ procedure sort(ref a[1:*] : int) # into non-decreasing order fa i := ub(a) downto 1 -> fa j := 1 to i-1 -> if a[j] > a[j+1] -> ##### animator #####v # swap the icon locations printf("jump %d %.3f %.3f\n", j, scale_x(j+1), scale_y(a[j])) printf("jump %d %.3f %.3f\n", j+1, scale_x(j), scale_y(a[j+1])) printf("swapid %d %d\n", j, j+1) # swap XTANGO ids of the icons ##### animator #####^ a[j] :=: a[j+1] # now swap array values ##### animator #####v [] else -> # swap the icon colors printf("fill %d outline\n", j) # make the smaller icon ... printf("color %d red\n", j) # ... outline red ... printf("color %d blue\n", j+1) # ... and the larger ... printf("fill %d solid\n", j+1) # ... solid blue ##### animator #####^ fi af ##### animator #####v # color the icon black to indicate it is in final position printf("color %d black\n", i) ##### animator #####^ af ##### animator #####v printf("fill 1 solid\n") ##### animator #####^ end sort var nums[1:N] : int ##### animator #####v # change coordinates so there is room around the edges printf("coords -0.05 -0.05 1.10 1.10\n") ##### animator #####^ fa i := 1 to N -> nums[i] := int(random(RANGE)+1); getarg(i+2, nums[i]) af printf("comment ") # for animator write("original numbers") print_array(nums) ##### animator #####v # display original numbers fa i := 1 to N -> printf("circle %d %.3f %.3f %.3f red outline\n", i, scale_x(i), scale_y(nums[i]), 0.75*scale_x(1)) af ##### animator #####^ sort(nums) printf("comment ") # for animator write("sorted numbers") print_array(nums) end sorter /* ............... Example compile and run(s) % sr -o sort_anim sort_anim.sr % ./sort_anim 3 10 comment Bubble sorting 3 numbers between 1 and 10 comment (or read POSITIVE numbers from command line) coords -0.05 -0.05 1.10 1.10 comment original numbers comment 5 9 4 circle 1 0.333 0.500 0.250 red outline circle 2 0.667 0.900 0.250 red outline circle 3 1.000 0.400 0.250 red outline fill 1 outline color 1 red color 2 blue fill 2 solid jump 2 1.000 0.900 jump 3 0.667 0.400 swapid 2 3 color 3 black jump 1 0.667 0.500 jump 2 0.333 0.400 swapid 1 2 color 2 black color 1 black fill 1 solid comment sorted numbers comment 4 5 9 % ./sort_anim 50 1000 | animator Press RUN ANIMATION button to begin XTANGO Version 1.53 Bubble sorting 50 numbers between 1 and 1000 (or read POSITIVE numbers from command line) original numbers 8 577 178 899 726 757 162 738 276 92 74 264 45 101 620 913 599 15 683 261 781 834 78 841 287 474 723 497 697 487 589 804 144 882 340 323 209 54 838 988 13 222 728 773 678 653 396 834 804 797 sorted numbers 8 13 15 45 54 74 78 92 101 144 162 178 209 222 261 264 276 287 323 340 396 474 487 497 577 589 599 620 653 678 683 697 723 726 728 738 757 773 781 797 804 804 834 834 838 841 882 899 913 988 */