예제 #1
0
void generateRand_3(int array[], int arraySize)
{
    Stopwatch stopWatch;
    int i;
    for (i=0; i<arraySize; i++)      //fill array first
    {
        array[i] = i+1;
    }
    stopWatch.start();
    for (i=1; i<arraySize; i++)      //now randomly swap stuff
    {
        swap(array[i], array[ array[i] = (int)(rand()*rand() % arraySize) ]);
    }
    stopWatch.stop();
    displayStats(arraySize, stopWatch.user(), stopWatch.user()/arraySize);
}
예제 #2
0
void generateRand_1(int array[], int arraySize)
{
    Stopwatch stopWatch;
    bool bad = false;       //a flag for checking to see if a certain number was already in another element.
    stopWatch.start();
    for (int i=0; i<arraySize; i++)
    {
        do
        {
            array[i] = (int)(rand()*rand() % arraySize);  //Assign a random value to array[i]
            bad = false;                        //set bad flag to false
            for (int j=0; j<i; j++)           //check each element up to current one
            {
                if ((int)array[i] == (int)array[j])       //if there is already one similar
                {
                    bad = true;                 //make bad flag true.
                }
            }
        } while ( bad );                        //keep assigning new numbers until it's not bad.
    }
    stopWatch.stop();
    displayStats(arraySize, stopWatch.user(), stopWatch.user()/(arraySize * arraySize * log10(arraySize)));
}
예제 #3
0
void generateRand_2(int array[], int arraySize)
{
    Stopwatch stopWatch;
    int i;                          //for the loops
    bool used[arraySize];            //array to hold whether a number has been used
    for (i=0; i<arraySize; i++) { used[i] = false; }     //initialize array

    for (i=0; i<arraySize; i++)
    {
        array[i] = (int)(rand()*rand() % arraySize);               //Assign random value
        if ( i > 0 )
        {
            //Check to see if that number was already used
            while ( used[ array[i] ] == true )
            {
                array[i] = (int)(rand()*rand() % arraySize);
            }
        }
        used[ array[i] ] = true;
    }
    stopWatch.stop();
    displayStats(arraySize, stopWatch.user(), stopWatch.user()/(arraySize * log10(arraySize)));
}