4

Preferrably with any non-virus open source license

Pratik Deoghare
  • 30,271
  • 27
  • 94
  • 143
skevar7
  • 969
  • 1
  • 10
  • 21

2 Answers2

6

here is one from wikibooks (This is Least Significant Digit based)

public void RadixSort(int[] a)
{  
    // our helper array 
    int[] t=new int[a.Length]; 

    // number of bits our group will be long 
    int r=4; // try to set this also to 2, 8 or 16 to see if it is 
             // quicker or not 

    // number of bits of a C# int 
    int b=32; 

    // counting and prefix arrays
    // (note dimensions 2^r which is the number of all possible values of a 
    // r-bit number) 
    int[] count=new int[1<<r]; 
    int[] pref=new int[1<<r]; 

    // number of groups 
    int groups=(int)Math.Ceiling((double)b/(double)r); 

    // the mask to identify groups 
    int mask = (1<<r)-1; 

    // the algorithm: 
    for (int c=0, shift=0; c<groups; c++, shift+=r)
    { 
        // reset count array 
        for (int j=0; j<count.Length; j++)
            count[j]=0;

        // counting elements of the c-th group 
        for (int i=0; i<a.Length; i++)
            count[(a[i]>>shift)&mask]++; 

        // calculating prefixes 
        pref[0]=0; 
        for (int i=1; i<count.Length; i++)
            pref[i]=pref[i-1]+count[i-1]; 

        // from a[] to t[] elements ordered by c-th group 
        for (int i=0; i<a.Length; i++)
            t[pref[(a[i]>>shift)&mask]++]=a[i]; 

        // a[]=t[] and start again until the last group 
        t.CopyTo(a,0); 
    } 
    // a is sorted 
}
nawfal
  • 62,042
  • 48
  • 302
  • 339
TheVillageIdiot
  • 38,082
  • 20
  • 126
  • 184
  • Oh, `count[(a[i]>>shift)&mask]++;` is so fk' readable! – abatishchev Feb 26 '15 at 05:20
  • @abatishchev please edit with more readable version. – TheVillageIdiot Feb 26 '15 at 05:32
  • 4
    It wasn't a comment about your answer but about that wiki article. A book to learn algorithms contains barely readable and understandable code. Just saying) – abatishchev Feb 26 '15 at 05:34
  • 3
    @abatishchev, to be honest, it was readable at the first sight, to me... it simply states that you want to increment the counter of a desired group.. variable shift is the size of the mask, so you essentially are moving the mask through your number and getting a group ID that way, I guess.. – Mladen B. Jun 10 '18 at 11:28
  • I found a much more legible example here: https://www.w3resource.com/csharp-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-10.php instead of using a constant of 32, this example uses 31 and -1 for the bounds. This example is also much cleaner with far fewer loops. – dyslexicanaboko Aug 06 '18 at 02:17
  • @ Mladen B What a smart man you are, well done. This looks like someone took the C version and ported it straight to C#. Using all those bitwise shifts looks like the code was originally written to run on a 70s C compiler, not the C# compiler of 2021. If confused why this is poor, see Eric Lippert response here: https://stackoverflow.com/questions/3059719/doubling-a-number-shift-left-vs-multiplication – Ash Mar 05 '21 at 08:05
0
    public static int[] radixSort(int[] ar)
    {
        int width = 0;
        foreach (int el in ar)
        {
            int numDigits = el.ToString().Length;
            if (numDigits > width)
                width = numDigits;
        }

        int md, n;

        Dictionary<int, LinkedList> queue = null;

        Action refreshQueue = () =>
       {
           queue = new Dictionary<int, LinkedList>();

           for (int i = 0; i <= 9; i++)
           {
               queue[i] = null;
           }
       };

        refreshQueue();

        for (int i = 1; i <= width; i++)
        {
            md = (int)Math.Pow(10, i); 
            n = md / 10; 

            foreach (int el in ar)
            {
                int ithPlace = (int)((el % md) / n);
                if (queue[ithPlace] == null)
                    queue[ithPlace] = new LinkedList(new LinkedListNode(el));
                else
                    queue[ithPlace].add(new LinkedListNode(el));
            }

            List<int> newArray = new List<int>();
            for (int k = 0; k <= 9; k++)
            {
                if (queue[k] != null)
                {
                    LinkedListNode head = queue[k].head;
                    while (head != null)
                    {
                        newArray.Add(head.value);
                        head = head.next;
                    }
                }
            }
            ar = newArray.ToArray();
            refreshQueue();
        }

        return ar;
    }
Lee.O.
  • 63
  • 5