REM OPL combsort demo global item%(500) local i%, j%, swaps%, top%, gap%, hold%, size% size%=500 REM Set up sample data randomize second i%=1 while i%<=size% item%(i%)=int(rnd*32768) i%=i%+1 endwh print "Sorting" gap%=size% do gap%=int(gap%*0.769231) if gap%=0 gap%=1 elseif (gap%=9) or (gap%=10) gap%=11 endif swaps%=0 top%=size%-gap% i%=1 while i%<=top% j%=i%+gap% if item%(i%)>item%(j%) hold%=item%(i%) item%(i%)=item%(j%) item%(j%)=hold% swaps%=-1 endif i%=i%+1 endwh until (gap%=1) and not swaps% print "Sorted" REM --- REM REM In practice you would sort an array containing the record number, REM having extracted another one with the sort key for each record. REM REM The advantages of CombSort are: its easy to code, it does not REM require much memory, its not recursive (so its stack usage is REM bounded), and its O(n*log(n)). REM REM For 500 items Combsort is about 50% slower than QuickSort, REM however, because it makes significantly more comparisons. So REM these should be made as fast as possible (hence the sort key REM array). In practice I have found it to be the only feasible sort REM in OPL on the Organiser. On the S3, the above routine takes 15 REM seconds, compared to about 11 seconds for QuickSort. REM