Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Venezia
{
class Utils
{
public:
template<typename T>
static T* SelectionSort(T arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < size; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
if (minIndex != i)
Swap(arr[i], arr[minIndex]);
}
return arr;
}
template<typename T>
static void Swap(T& obj1, T& obj2)
{
T tempCopy = obj1;
obj1 = obj2;
obj2 = tempCopy;
}
// Precondition: prob is between 0.0 and 1.0 inclusive.
// Returns: generates random probability between 0.0 and 1.0 and returns true if randomProb
// is <= prob, else false. Each invocation of this function has a (prob*100.0) percent chance
// of returning true.
bool Probability(float prob)
{
float randomProb = (RAND_MAX - rand()) / static_cast<float>(RAND_MAX);
return (randomProb <= prob) ? true : false;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.