From 60e49bfc46dbaa048f15e05df9425749df5f4982 Mon Sep 17 00:00:00 2001 From: Waqar Hassan Khan Date: Sat, 18 Sep 2021 21:39:49 +0600 Subject: [PATCH] chore(CSharp): Added counting sort (#470) * added counting sort implementation * updated readme * added test case * code refactored --- algorithms/CSharp/README.md | 3 +- algorithms/CSharp/src/Sorts/counting-sort.cs | 69 +++++++++++++++++++ algorithms/CSharp/test/Sorts/counting-sort.cs | 34 +++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 algorithms/CSharp/src/Sorts/counting-sort.cs create mode 100644 algorithms/CSharp/test/Sorts/counting-sort.cs diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 081b254e..f16eb7f8 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -6,6 +6,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ 1. [Bubble Sort](src/Sorts/bubble-sort.cs) 2. [Insertion Sort](src/Sorts/insertion-sort.cs) 3. [Selection Sort](src/Sorts/selection-sort.cs) +4. [Counting Sort](src/Sorts/counting-sort.cs) ## Strings - [Palindrome](src/Strings/palindrome.cs) @@ -20,7 +21,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/ ## Queues - [Queue Implementation Using Two Stacks](src/Queues/queue-implementation-using-two-stacks.cs) -## Recusrsion +## Recursion - [Factorial](src/Recursion/factorial.cs) ## Graph diff --git a/algorithms/CSharp/src/Sorts/counting-sort.cs b/algorithms/CSharp/src/Sorts/counting-sort.cs new file mode 100644 index 00000000..73da37fa --- /dev/null +++ b/algorithms/CSharp/src/Sorts/counting-sort.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Algorithms.Sorts +{ + public class CountingSort + { + public static List Sort(List numbers) + { + int maxNumber = 0, minNumber = 0; + foreach(int number in numbers) + { + maxNumber = number > maxNumber ? number : maxNumber; + minNumber = number < minNumber ? number : minNumber; + } + + int[] mx = Enumerable.Repeat(0, maxNumber + 1).ToArray(); + int[] mn = Enumerable.Repeat(0, minNumber * -1 + 1).ToArray(); + + foreach(int number in numbers) + { + if(number >= 0) + { + mx[number]++; + } + + else + { + mn[number * -1]++; + } + } + + List sortedList = new List(); + + if(minNumber < 0) + { + for (int i = minNumber; i < 0; i++) + { + while(mn[i * -1] > 0) + { + sortedList.Add(i); + mn[i * -1]--; + } + } + } + + for(int i = 0; i <= maxNumber; i++) + { + while(mx[i] > 0) + { + sortedList.Add(i); + mx[i]--; + } + } + + return sortedList; + } + + public static void Main() + { + List numbers = new List { 9, 1, -1, 10, 12, 2, 0, 0, -2, -9 }; + List answer = Sort(numbers); + + Console.WriteLine(string.Join(", ", answer)); + } + } +} diff --git a/algorithms/CSharp/test/Sorts/counting-sort.cs b/algorithms/CSharp/test/Sorts/counting-sort.cs new file mode 100644 index 00000000..9275df66 --- /dev/null +++ b/algorithms/CSharp/test/Sorts/counting-sort.cs @@ -0,0 +1,34 @@ +using NUnit.Framework; +using System.Collections.Generic; + +namespace Algorithms.Tests.Sorts +{ + [TestFixture] + public class CountingSort + { + static object[] TestCasesForCountingSort = + { + new object[] { + new List{ 0, 19, 12, 22, 107, 118, 0, 1, 2}, + "0, 0, 1, 2, 12, 19, 22, 107, 118" + }, + + new object[] { + new List{ 10, 11, 19, 0, -1, -19, -12, 1, 2, 1, 16, -100}, + "-100, -19, -12, -1, 0, 1, 1, 2, 10, 11, 16, 19" + }, + + new object[] { + new List{ -1, -2, -3, -4, -5, -10}, + "-10, -5, -4, -3, -2, -1" + }, + }; + + [TestCaseSource(nameof(TestCasesForCountingSort))] + public void TestCountingSort_ShouldGetExpected(List numbers, string expected) + { + List results = Algorithms.Sorts.CountingSort.Sort(numbers); + Assert.AreEqual(string.Join(", ", results), expected); + } + } +}