w3resource
C# Sharp Programming Exercises

C# Sharp Searching and Sorting Algorithm Exercises: Radix sort

C# Sharp Searching and Sorting Algorithm: Exercise-10 with Solution

Write a C# Sharp program to sort a list of elements using Radix sort algorithm.

Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.

An Example :

Original, unsorted list : 272, 45, 75, 81, 501, 2, 24, 66

Sorting by least significant digit (1s place) gives :

272, 81, 5012, 24, 45, 75, 66

Here we keep 501 before 2, because 501 occurred before 2 in the original list, and similarly for pairs 272 & 81 and 45 & 75.

Sorting by next digit (10s place) gives :

501, 2, 24, 45, 66, 272, 75, 9

Notice that 501 again comes before 2 as 501 comes before 2 in the previous list.

Sorting by most significant digit (100s place) gives:

2, 24, 45, 66, 75, 81, 272, 501

It is important to realize that each of the above steps requires just a single pass over the data, since each item can be placed in its correct bucket without having to be compared with other items.

Some radix sort implementations allocate space for buckets by first counting the number of keys that belong in each bucket before moving keys into those buckets. The number of times that each digit occurs is stored in an array. Consider the previous list of keys viewed in a different way :

272, 045, 075, 081, 002, 024, 501, 066

The first counting pass starts on the least significant digit of each key, producing an array of bucket sizes :

  • 2 (bucket size for digits of 0: 272, 081)
  • 2 (bucket size for digits of 2: 002, 501)
  • 1 (bucket size for digits of 4: 024)
  • 2 (bucket size for digits of 5: 045, 075)
  • 1 (bucket size for digits of 6: 066)

A second counting pass on the next more significant digit of each key will produce an array of bucket sizes :

  • 2 (bucket size for digits of 0: 002, 501)
  • 1 (bucket size for digits of 2: 024)
  • 1 (bucket size for digits of 4: 045)
  • 1 (bucket size for digits of 6: 066)
  • 2 (bucket size for digits of 7: 272, 075)
  • 1 (bucket size for digits of 9: 081

A third and final counting pass on the most significant digit of each key will produce an array of bucket sizes :

  • 6 (bucket size for digits of 0: 002, 024, 045, 066, 075, 081)
  • 1 (bucket size for digits of 1: 272)
  • 1 (bucket size for digits of 8: 501)

Sample Solution:-

C# Sharp Code:

using System;
 namespace Radix_Sort
{
    class Program
    {
        static void Sort(int[] arr)
        {
            int i, j;
            int[] tmp = new int[arr.Length];
            for (int shift = 31; shift > -1; --shift)
            {
                j = 0;
                for (i = 0; i < arr.Length; ++i)
                {
                    bool move = (arr[i] << shift) >= 0;
                    if (shift == 0 ? !move : move)   
                        arr[i-j] = arr[i];
                    else                             
                        tmp[j++] = arr[i];
                }
                Array.Copy(tmp, 0, arr, arr.Length-j, j);
            }
        }
        static void Main(string[] args)
        {
            
			int[] arr = new int[] { 2, 5, -4, 11, 0, 18, 22, 67, 51, 6  };
			Console.WriteLine("\nOriginal array : ");
			foreach (var item in arr)
            {
                Console.Write(" " + item);    
            }

            Sort(arr);
			Console.WriteLine("\nSorted array : ");
			foreach (var item in arr)
            {
                Console.Write(" " + item);    
            }
           Console.WriteLine("\n");
        }
    }
}

Sample Output:

Original array :                                                                                              
 2 5 -4 11 0 18 22 67 51 6                                                                                    
Sorted array :                                                                                                
 -4 0 2 5 6 11 18 22 51 67 

Flowchart:

C# Sharp Searching and Sorting Algorithm Exercises: Radix sort

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to sort a list of elements using Quick sort
Next: Write a C# Sharp program to sort a list of elements using the selection sort algorithm.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming