I'm trying to code a Merge Sort that sorts through the numbers 1-10 and puts them in order...How can I code it so that it doesn't have just random numbers but a random order of 1-10?
This is what I have that gives me my numbers, Ive been messing with it but I haven't figured it out. Any Suggestions?
1 2 3 4 5 6 7 8 9
MergeSort::MergeSort(int vectorSize)
{
size = (vectorSize > 0 ? vectorSize : 10); //validate vectorSize
srand(time(0)); //seeds random number generator based on time
//constructor fill vector with random integers
for(int i = 0; i < size; i++)
data.push_back(10 + rand() % 90);
}//end Merge constructor
If you need all of the numbers 1-10 and you just want to put them in a random order, don't use rand(). Just start with a vector of 1-10 and then do a random shuffle.
1 2 3 4 5
for (int i = 1; i <= size; i++)
data.push_back(i); // Fill the vector with numbers 1 to size
// Now everyday I'm shuffling
std::random_shuffle ( data.begin(), data.end() );
As already was said you can use standard algorithm std::random_shuffle declared in header <algorithm> and also to use standard algorithm std::iota declared in header <numeric> to set initilal values.
Let assume that the range of random numbers is specified by some constant
1 2 3 4 5 6
constint N = 10;
std:;vector<int> v( N );
std::iota( v,begin(), v.end(), 1 );
std::random_shuffle( v.begin(), v.end() );