Ejemplo n.º 1
0
/*
 * Function: RunSortTrial
 * ----------------------
 * Runs the sorting time trial for the specified pqueue size.
 * Reports results to cout.
 */
void RunSortTrial(int size)
{
	int *array = new int[size];
	for (int i = 0; i < size; i++)		// put random data into array
		array[i] = RandomInteger(1, size);
		
    cout << "Time to pqsort random sequence of " << size << " elements: " << flush;
	double start = GetCurrentTime();
	PQSort(array, size);
    cout << GetCurrentTime() - start << " msecs" << endl;
	
    cout << "Time to pqsort sorted sequence of " << size << " elements: " << flush;
	for (int j = 0; j < size; j++)		// put data in array already sorted 
		array[j] = j;
	start = GetCurrentTime();
	PQSort(array, size);
    cout << GetCurrentTime() - start << " msecs" << endl;
	
    cout << "Time to pqsort reverse-sorted sequence of " << size << " elements: " << flush;
	for (int k = 0; k < size; k++)		// put data in array already reverse sorted 
		array[k] = size - k;
	start = GetCurrentTime();
	PQSort(array, size);
    cout << GetCurrentTime() - start << " msecs" << endl;

	delete [] array;
}
Ejemplo n.º 2
0
/*
 * Function: PQueueSortTest
 * Usage: PQueueSortTest();
 * ------------------------
 * Tests the use of the priority queue to implement a sort algorithm.  Enqueues
 * a bunch of values into pqueue and then pull them out using dequeueMax to arrange in
 * array in sorted order.  Tries it with random data inserted and then two more times
 * with data inserted in sorted order (both forward and backward) to learn how
 * the performance is affected by already sorted data.
 */
void PQueueSortTest()
{
	const int SortSize = 500;
	int array[SortSize];
	
	cout << endl << "-----------   Testing use of pqueue to sort  -----------" << endl;
	
	cout << "Enqueuing " << SortSize << " numbers into pqueue in increasing order." << endl;
	for (int i = 0; i < SortSize; i++) array[i] = i;
	PQSort(array, SortSize);
	cout << "Using dequeue to pull out numbers in sorted order.  Are they sorted? " <<
		ArrayIsSorted(array, SortSize) << endl;

	cout << "Enqueuing " << SortSize << " random values into the pqueue." << endl;
	for (int j = 0; j < SortSize; j++) array[j] = RandomInteger(1, 1000);
	PQSort(array, SortSize);
	cout << "Using dequeue to pull out numbers in sorted order.  Are they sorted? " <<
		ArrayIsSorted(array, SortSize) << endl;
	
	cout << endl << "Hit return to continue: ";
	GetLine();
}