Hello,
I have a function to sort a vector, the function is inside a while loop that loops until the vector is sorted.
Here is the function:
void bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
for (int n = 0; n < sortElements.size() - 1; n++) {
if (sortElements[n].value > sortElements[n + 1].value) {
// Swap positions
auto currElement = sortElements[n];
auto tempElement = sortElements[n + 1];
sortElements[n + 1] = currElement;
sortElements[n] = tempElement;
}
// Wait timeSleep microseconds between iterations
sf::sleep(sf::microseconds(timeSleep));
}
}
I wanted to implement a time sleep between each cycle, works good above 1000 microseconds (1ms), I also tested if there is any difference in time between 1000 and 1500 microseconds and there is a difference in time, so everything works great for now. But, when timeSleep is 999 or less, the time that each cycle takes is the same, from 1 to 999 microseconds (just like the sleep being skipped).
I also tried this alternative, but does something similar, under a certain range just rounds up to n miliseconds:
std::this_thread::sleep_for(std::chrono::microseconds(timeSleep));
The function is being executed in a detached thread.
ty!