
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort


Sort an array of numbers (of any convenient size) into ascending order using   Patience sorting.



Related task

   Longest increasing subsequence


/*REXX program sorts a list of things (or items) using the  patience sort  algorithm.   */
parse arg xxx;     say ' input: '      xxx       /*obtain a list of things from the C.L.*/
n= words(xxx);     #= 0;       !.= 1             /*N:  # of things;  #:  number of piles*/
@.=                                              /* [↓]  append or create a pile  (@.j) */
   do i=1  for n;              q= word(xxx, i)   /* [↓]  construct the piles of things. */
                do j=1  for #                    /*add the   Q   thing (item) to a pile.*/
                if q>word(@.j,1)  then iterate   /*Is this item greater?   Then skip it.*/
                @.j= q  @.j;           iterate i /*add this item to the top of the pile.*/
                end   /*j*/                      /* [↑]  find a pile, or make a new pile*/
   #= # + 1                                      /*increase the pile count.             */
   @.#= q                                        /*define a new pile.                   */
   end                /*i*/                      /*we are done with creating the piles. */
$=                                               /* [↓]   build a thingy list from piles*/
   do k=1  until  words($)==n                    /*pick off the smallest from the piles.*/
   _=                                            /*this is the smallest thingy so far···*/
          do m=1  for  #;     z= word(@.m, !.m)  /*traipse through many piles of items. */
          if z==''  then iterate                 /*Is this pile null?    Then skip pile.*/
          if _==''  then _= z                    /*assume this one is the low pile value*/
          if _>=z   then do;  _= z;  p= m;  end  /*found a low value in a pile of items.*/
          end   /*m*/                            /*the traipsing is done, we found a low*/
   $= $ _                                        /*add to the output thingy  ($)  list. */
   !.p= !.p + 1                                  /*bump the thingy pointer in pile  P.  */
   end          /*k*/                            /* [↑]  each iteration finds a low item*/
                                                 /* [↓]  string  $  has a leading blank.*/
say 'output: '       strip($)                    /*stick a fork in it,  we're all done. */