If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# program to perform radix sort with the output.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, We have shared the program output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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(); } } } |
Enter the Number of Records : 5
Enter Record 1 : 95
Enter Record 2 : 70
Enter Record 3 : 50
Enter Record 4 : 27
Enter Record 5 : 45
The Sorted Numbers are :
27
45
50
70
95
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.