Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sort

16 views
Skip to first unread message

Sarantos Kapidakis

unread,
May 19, 1990, 6:49:46 AM5/19/90
to

Here is the requested sort function
example of usage is:
[1 20 12 7 33 12 8 99 21 22 1 10 20 10 22 40] {gt} insertionsort

%!
% sorts the array on the stack in place
% If you want to sort a duplicate, use: dup length array copy
% The order of the elements on the stack is: array function v j

% array greater_function insertionsort array
/insertionsort
{ 1 1 3 index length 1 sub
{ 2 index 1 index get exch % v, j
{ dup 0 eq {exit} if
3 index 1 index 1 sub get 2 index 4 index exec
{3 index 1 index 2 copy 1 sub get put 1 sub}
{exit} ifelse
} loop
exch 3 index 3 1 roll put
} for
pop
} def

% A more readable version was:
% /InsertionSortDict 5 dict def
% /insertionsort
% { InsertionSortDict begin
% /f_gt exch def
% /a exch def
% 1 1 a length 1 sub
% { /j exch def /v a j get def
% { j 0 eq {exit} if
% a j 1 sub get v f_gt
% {a j a j 1 sub get put /j j 1 sub def} {exit} ifelse
% } loop
% a j v put
% } for
% a
% end
% } def

% Sarantos Kapidakis (sara...@princeton.edu)

0 new messages