C# Program to Implement Radix Sort

This is a C# Program to perform radix sort.

Problem Description

This C# Program Performs Radix Sort.

Problem Solution

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

Program/Source Code

Here is source code of the C# program which performs Radix Sort. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform Radix Sort
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication2
{
    class Example
    {
        private int[] data;
        private IList<IList<int>> digits = new List<IList<int>>();
        private int maxLength = 0;
        public Example()
        {
            for (int i = 0; i < 10; i++)
            {
                digits.Add(new List<int>());
            }
            Console.Write("Enter the Number of Records : ");
            int count = int.Parse(Console.ReadLine());
            data = new int[count];
            Console.ReadLine();
            for (int i = 0; i < count; i++)
            {
                Console.Write("Enter Record {0} : ", i + 1);
 
                data[i] = int.Parse(Console.ReadLine());
 
                if (maxLength < data[i].ToString().Length)
                    maxLength = data[i].ToString().Length;
            }
        }
 
        public void RadixSort()
        {
           for (int i = 0; i < maxLength; i++)
           {
               for (int j = 0; j < data.Length; j++)
               {
                  int digit=(int)((data[j] % Math.Pow(10, i + 1)) / Math.Pow(10, i));
 
                  digits[digit].Add(data[j]);
                }
 
                int index = 0;
                for (int k = 0; k < digits.Count; k++)
                {
                    IList<int> selDigit = digits[k];
 
                    for (int l = 0; l < selDigit.Count; l++)
                    {
                        data[index++] = selDigit[l];
                    }
                }
                ClearDigits();
           }
           printSortedData();
        }
 
        private void ClearDigits()
        {
            for (int k = 0; k < digits.Count; k++)
            {
                digits[k].Clear();
            }
        }
 
        public void printSortedData()
        {
            Console.WriteLine("The Sorted Numbers are : ");
            for (int i = 0; i < data.Length; i++)
            {
                Console.WriteLine(data[i]);
            }
        }
        static void Main(string[] args)
        {
            new Example().RadixSort();
 
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program, we are reading the number of records to perform radix sort using ‘count’ variable. Radix sort is an example of a stable sorting algorithm. A sorting algorithm is said to be stable if two objects with the same keys appear in the exact same order in the sorted output array as they appear in the input array.

advertisement
advertisement

Using for loop we are entering the coefficient values of an array. If condition statement is used to check the value of ‘maxlength’ variable is less than date[] array variable. If the condition is true then execute the statement. Assign the value of ‘data[]’ array variable to the ‘maxlength’ variable.

In RadixSort() method for loop is used to sort data with integer keys by grouping keys by the individual digits which share the same significant position and value. The ClearDigits() method is used to clear the digits in the array using clear() function. Using for loop in printSortedData() method print the sorted numbers.

Runtime Test Cases
 
Enter the  Number of Records : 5
Enter Record 1 : 54
Enter Record 2 : 53
Enter Record 3 : 15
Enter Record 4 : 27
Enter Record 5 : 75
The Sorted Numbers are :
15
27
53
54
75

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.