chore(CSharp): Added counting sort (#470)
* added counting sort implementation * updated readme * added test case * code refactoredpull/472/head
parent
0661f09ae8
commit
60e49bfc46
|
@ -6,6 +6,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
|
||||||
1. [Bubble Sort](src/Sorts/bubble-sort.cs)
|
1. [Bubble Sort](src/Sorts/bubble-sort.cs)
|
||||||
2. [Insertion Sort](src/Sorts/insertion-sort.cs)
|
2. [Insertion Sort](src/Sorts/insertion-sort.cs)
|
||||||
3. [Selection Sort](src/Sorts/selection-sort.cs)
|
3. [Selection Sort](src/Sorts/selection-sort.cs)
|
||||||
|
4. [Counting Sort](src/Sorts/counting-sort.cs)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
- [Palindrome](src/Strings/palindrome.cs)
|
- [Palindrome](src/Strings/palindrome.cs)
|
||||||
|
@ -20,7 +21,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
|
||||||
## Queues
|
## Queues
|
||||||
- [Queue Implementation Using Two Stacks](src/Queues/queue-implementation-using-two-stacks.cs)
|
- [Queue Implementation Using Two Stacks](src/Queues/queue-implementation-using-two-stacks.cs)
|
||||||
|
|
||||||
## Recusrsion
|
## Recursion
|
||||||
- [Factorial](src/Recursion/factorial.cs)
|
- [Factorial](src/Recursion/factorial.cs)
|
||||||
|
|
||||||
## Graph
|
## Graph
|
||||||
|
|
|
@ -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<int> Sort(List<int> 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<int> sortedList = new List<int>();
|
||||||
|
|
||||||
|
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<int> numbers = new List<int> { 9, 1, -1, 10, 12, 2, 0, 0, -2, -9 };
|
||||||
|
List<int> answer = Sort(numbers);
|
||||||
|
|
||||||
|
Console.WriteLine(string.Join(", ", answer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<int>{ 0, 19, 12, 22, 107, 118, 0, 1, 2},
|
||||||
|
"0, 0, 1, 2, 12, 19, 22, 107, 118"
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[] {
|
||||||
|
new List<int>{ 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<int>{ -1, -2, -3, -4, -5, -10},
|
||||||
|
"-10, -5, -4, -3, -2, -1"
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
[TestCaseSource(nameof(TestCasesForCountingSort))]
|
||||||
|
public void TestCountingSort_ShouldGetExpected(List<int> numbers, string expected)
|
||||||
|
{
|
||||||
|
List<int> results = Algorithms.Sorts.CountingSort.Sort(numbers);
|
||||||
|
Assert.AreEqual(string.Join(", ", results), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue