/frames 31 def %include /u/rs/book3/pslib/tools.ps %include /u/rs/book3/pslib/example.ps /hsort { /incr exch def incr 1 N 1 sub { /lll exch def lll incr neg incr { /xxx exch def xxx incr sub xxx compare { exit } if xxx xxx incr sub compareswap /xxx xxx incr sub def } for xxx lll SUBFILEx } for } def /SUBFILEx { /rr exch def /ll exch def LL 1 RR { dup dup dup ll incr sub le exch rr gt or exch rr exch sub incr mod 0 ne or { SqGrL }{ SqWhL } ifelse exch PT } for nextframe } def LL RR SUBFILE 13 hsort nextframe 4 hsort nextframe 1 hsort LL RR SUBFILE ---------------------------- T 22.7pc Shellsort example Sorting a file by 13-sorting \itpc{top}, then 4-sorting \itpc{middle}, then 1-sorting \itpc{bottom} does not involve many comparisons (as indicated by the unshaded elements). The last pass is just insertion sort, but no element has to move very far because of the order in the file due to the first two passes.