This is my first post and any help would be appreciated. I'm writing a program that reads a file of integers of unknown length, then allows the user to choose 1 of 5 kinds of sorts. At the end. the time it took to sort the list will be displayed. The problem has been finding something to time it in ms. What I have below is the closest I've gotten but at times still shows 0 seconds, other times .06, or .11 (all my testing so far has been with a selection sort and bubble sort of 200 integers).
I hope I got the code tags right. If there's any other methods of timing anyone knows that are more consistent and accurate I would appreciate it. Thanks!Code:void SelectionSort( vector <int>& listOfData, double& duration) { int temp; int counter; int index; int minIndex; clock_t start; clock_t finish; start = clock(); // grabs beginning count for (counter = 0; counter < listOfData.size(); counter++) { minIndex = counter; for (index = counter + 1; index < listOfData.size(); index++) { if (listOfData[index] < listOfData[minIndex]) minIndex = index; } temp = listOfData[counter]; listOfData[counter] = listOfData[minIndex]; listOfData[minIndex] = temp; } finish = clock(); // grabs ending count duration = double (finish - start) / CLOCKS_PER_SEC; }
Also on a somewhat unrelated topic. With vectors, is there an upper range limit of elements that a vector can hold? Is it necessary to do checks for out of bounds? And if so, how might I do that without knowing the number of elements I'll be adding? I'll appreciate any help. Thanks!