Updated index.md: C++ implementation for count sort (#26609)

* Update index.md

Added a C++ implementation to count sort using std::vector

* fix: added code block syntax
This commit is contained in:
ngutierrez31
2019-02-19 21:49:13 -05:00
committed by Randell Dawson
parent 4ba7381167
commit 1b4122cfef

View File

@ -53,4 +53,21 @@ for (i=0; i < numbers.length; i++) {
}
```
### C++ Implementation
```cpp
#include <iostream>
void countSort(int upperBound, int lowerBound, std::vector<int> numbersToSort) //lower and upper bounds of numbers in vector
{
int range = upperBound - lowerBound; //create a range large enough to get every number between the min and max
std::vector<int> counts (range); //initialize of counts of the size of the range
std::fill(counts.begin(), counts.end(), 0); //fill vector of zeros
for (int i = 0; i < numbersToSort.size(); i++)
{
int index = numbersToSort[i] - lowerBound; //For example, if 5 is the lower bound and numbersToSort[i] is 5. index will be 0 and the counts[index]+= 1; //count of 5 will be stored in counts[0]
}
std::cout << counts << std::endl;
}
```