Skip to content

Commit

Permalink
Fix iiitv#611: Add Counting Sort [C#] (iiitv#612)
Browse files Browse the repository at this point in the history
* Implemented Trie C#

* Added check mark under Trie c#

* Fixed syntax

* Implemented Counting Sort [C#]

* Added counting sort to readme

* Update README.md

* Code refactoring based on PR feedback

* Fixed Travis indent size error
  • Loading branch information
DimaMukhin authored and singhpratyush committed Dec 15, 2017
1 parent 63ac50b commit 4cec987
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [:white_check_mark:](binary_search/binary_search.c) | | [:white_check_mark:](binary_search/BinarySearch.java) | [:white_check_mark:](binary_search/binary_search.py) | [:white_check_mark:](binary_search/binary_search.go) | [:white_check_mark:](binary_search/binarySearch.js) | [:white_check_mark:](binary_search/BinarySearch.cs) |
| [Breadth First Search](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [:white_check_mark:](breadth_first_search/BreadthFirstSearch.java) | | | | |
| [Coin Change Problem](http://www.algorithmist.com/index.php/Coin_Change) | [:white_check_mark:](coin_change_problem/coin_change_problem.c) | | [:white_check_mark:](coin_change_problem/CoinChangeProblem.java) | [:white_check_mark:](coin_change_problem/coin_change_problem.py) | [:white_check_mark:](coin_change_problem/coin_change_problem.go) | [:white_check_mark:](coin_change_problem/coinChangeProblem.js) | |
| [Counting Sort](http://www.geeksforgeeks.org/counting-sort/)| [:white_check_mark:](counting_sort/counting_sort.c) | | [:white_check_mark:](counting_sort/CountingSort.java) | [:white_check_mark:](counting_sort/counting_sort.py) | [:white_check_mark:](counting_sort/counting_sort.go) | [:white_check_mark:](counting_sort/countingSort.js) | |
| [Counting Sort](http://www.geeksforgeeks.org/counting-sort/)| [:white_check_mark:](counting_sort/counting_sort.c) | | [:white_check_mark:](counting_sort/CountingSort.java) | [:white_check_mark:](counting_sort/counting_sort.py) | [:white_check_mark:](counting_sort/counting_sort.go) | [:white_check_mark:](counting_sort/countingSort.js) | [:white_check_mark:](counting_sort/CountingSort.cs) |
| [Depth First Traversal](http://www.geeksforgeeks.org/depth-first-traversal-for-a-graph/) | | | [:white_check_mark:](depth_first_traversal/DepthFirstTraversal.java) | [:white_check_mark:](depth_first_traversal/DepthFirstTraversal.py) | | | |
| [Dijkstra Algorithm](https://en.wikipedia.org/wiki/Dijkstra's_algorithm) | [:white_check_mark:](dijkstra/dijkstra.c) | | [:white_check_mark:](dijkstra/Dijkstra.java) | [:white_check_mark:](dijkstra/dijkstra.py) | | | |
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.c) | | [:white_check_mark:](euclidean_gcd/EuclideanGCD.java) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.py) | [:white_check_mark:](euclidean_gcd/euclidean_gcd.go) | [:white_check_mark:](euclidean_gcd/euclideanGCD.js) | [:white_check_mark:](euclidean_gcd/EuclideanGCD.cs) |
Expand Down Expand Up @@ -63,7 +63,6 @@ Community (college) maintained list of Algorithms and Data Structures implementa




## How to run them

| Language | Steps |
Expand Down
43 changes: 43 additions & 0 deletions counting_sort/CountingSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

public class CountingSort
{

/**
* Sorts the array using counting sort
* Time Complexity: O(n + k) where n is the number of elements in the array and k is the range of input
* Space Complexity: O(n + k)
*/
public static int[] DoCountingSort(int[] arr)
{
// finding the maximum value in the array
int maxValue = Int32.MinValue;
foreach (int num in arr)
maxValue = Math.Max(maxValue, num);

// counting the frequency of each element in the array
int[] frequency = new int[maxValue + 1];
foreach (int num in arr)
frequency[num]++;

// modifying each element in the frequency array to the total of all the elements before it
for (int i = 1; i < frequency.Length; i++)
frequency[i] += frequency[i - 1];

// constructing the sorted array based on the freqency of each element in ascending order
int[] sortedArr = new int[arr.Length];
for (int i = 0; i < arr.Length; i++) {
sortedArr[frequency[arr[i]] - 1] = arr[i];
frequency[arr[i]]--;
}
return sortedArr;
}

public static void Main()
{
int[] arr = new int[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] sortedArr = DoCountingSort(arr);
foreach (int num in sortedArr)
Console.WriteLine(num);
}
}

0 comments on commit 4cec987

Please sign in to comment.