
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


The numerical rank of competitors in a competition shows if one is better than, equal to, or worse than another based on their results in a competition.

The numerical rank of a competitor can be assigned in several different ways.



Task

The following scores are accrued for all competitors of a competition (in best-first order):

44 Solomon
42 Jason
42 Errol
41 Garry
41 Bernard
41 Barry
39 Stephen
For each of the following ranking methods, create a function/method/procedure/subroutine... that applies the ranking method to an ordered list of scores with scorers:

 Standard. (Ties share what would have been their first ordinal number).
 Modified. (Ties share what would have been their last ordinal number).
 Dense. (Ties share the next available integer). 
 Ordinal. ((Competitors take the next available integer. Ties are not treated otherwise).
 Fractional. (Ties share the mean of what would have been their ordinal numbers).


See the wikipedia article for a fuller description.

Show here, on this page, the ranking of the test scores under each of the numbered ranking methods.




F mc_rank([(Int, String)] iterable)
   ‘Modified competition ranking’
   [(Float, (Int, String))] r
   V lastresult = -1
   [(Int, String)] fifo
   L(item) iterable
      I item[0] == lastresult
         fifo [+]= item
      E
         V n = L.index
         L !fifo.empty
            r.append((n, fifo.pop(0)))
         lastresult = item[0]
         fifo [+]= item
   L !fifo.empty
      r.append((iterable.len, fifo.pop(0)))
   R r
 
F sc_rank([(Int, String)] iterable)
   ‘Standard competition ranking’
   [(Float, (Int, String))] r
   V lastresult = -1
   V lastrank = -1
   L(item) iterable
      I item[0] == lastresult
         r.append((lastrank, item))
      E
         V n = L.index + 1
         r.append((n, item))
         lastresult = item[0]
         lastrank = n
   R r
 
F d_rank([(Int, String)] iterable)
   ‘Dense ranking’
   [(Float, (Int, String))] r
   V lastresult = -1
   V lastrank = 0
   L(item) iterable
      I item[0] == lastresult
         r.append((lastrank, item))
      E
         lastresult = item[0]
         lastrank++
         r.append((lastrank, item))
   R r
 
F o_rank([(Int, String)] iterable)
   ‘Ordinal ranking’
   R enumerate(iterable, 1).map((i, item) -> ((Float(i), item)))
 
F f_rank([(Int, String)] iterable)
   ‘Fractional ranking’
   [(Float, (Int, String))] r
   V last = -1
   [(Int, (Int, String))] fifo
   L(item) iterable
      I item[0] != last
         I !fifo.empty
            V mean = Float(sum(fifo.map(f -> f[0]))) / fifo.len
            L !fifo.empty
               r.append((mean, fifo.pop(0)[1]))
      last = item[0]
      fifo.append((L.index + 1, item))
   I !fifo.empty
      V mean = sum(fifo.map(f -> f[0])) / fifo.len
      L !fifo.empty
         r.append((mean, fifo.pop(0)[1]))
   R r
 
V scores = [(44, ‘Solomon’),
            (42, ‘Jason’),
            (42, ‘Errol’),
            (41, ‘Garry’),
            (41, ‘Bernard’),
            (41, ‘Barry’),
            (39, ‘Stephen’)]
 
print("\nScores to be ranked (best first):")
L(n, s) scores
   print(‘        #2 #.’.format(n, s))
 
L(ranker, ranking_method) [(sc_rank, ‘Standard competition ranking’),
                           (mc_rank, ‘Modified competition ranking’),
                            (d_rank, ‘Dense ranking’),
                            (o_rank, ‘Ordinal ranking’),
                            (f_rank, ‘Fractional ranking’)]
   print("\n#.:".format(ranking_method))
   L(rank, score) ranker(scores)
      print(‘  #3, (#., #.)’.format(rank, score[0], score[1]))