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
This is a C# Program to perform radix sort.
This C# Program Performs Radix Sort.
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
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(); } } }
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.
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.
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