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++)
{
}
Console.Write("Enter the Number of Records : ");
data = new int[count];
for (int i = 0; i < count; i++)
{
Console.Write("Enter Record {0} : ", i + 1);

if (maxLength < data[i].ToString().Length)
maxLength = data[i].ToString().Length;
}
}

{
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));

}

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)
{

}
}
}```
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.

Note: Join free Sanfoundry classes at Telegram or Youtube

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.

Take C# Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
If you wish to look at all C# Programming examples, go to 1000 C# Programs.