Example #1
0
void PQueueSortTest(void)
{
	int i;
	pqueueADT pq;
	int array[SORT_SIZE];

	printf("\n-----------   Testing use of pqueue to sort  -----------\n");
	pq = NewPQueue();

	printf("Enqueuing %d numbers into pqueue in increasing order.", SORT_SIZE);
	for (i = 0; i < SORT_SIZE; i++) array[i] = i;
	HeapSort(pq, array, SORT_SIZE);
	printf("\nUsing dequeue to pull out numbers in sorted order.  Are they sorted? %s\n",
		ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE");
        printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE");

	printf("\nEnqueuing %d numbers into pqueue in decreasing order.", SORT_SIZE);
	for (i = 0; i < SORT_SIZE; i++) array[i] = SORT_SIZE - i;
	HeapSort(pq, array, SORT_SIZE);
	printf("\nUsing dequeue to pull out numbers in sorted order.  Are they sorted? %s\n",
		ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE");
        printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE");

	printf("\nEnqueuing %d random values into the pqueue.\n", SORT_SIZE);
        for (i = 0; i < SORT_SIZE; i++) array[i] = RandomInteger(1, 1000);
	HeapSort(pq, array, SORT_SIZE);
	printf("Using dequeue to pull out numbers in sorted order.  Are they sorted? %s\n",
		ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE");
        printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE");

	printf("\nEnqueuing %d random possibly negative values into the pqueue.\n", SORT_SIZE);
        for (i = 0; i < SORT_SIZE; i++) array[i] = RandomInteger(-1000, 1000);
	HeapSort(pq, array, SORT_SIZE);
	printf("Using dequeue to pull out numbers in sorted order.  Are they sorted? %s\n",
		ArrayIsSorted(array, SORT_SIZE) ? "TRUE" : "FALSE");
        printf("PQueue should be empty. Is it empty? %s\n", IsEmpty(pq) ? "TRUE" : "FALSE");

	FreePQueue(pq);
	printf("Hit return to continue: ");
	{
		string s = GetLine();
		FreeBlock(s);
	}
}
/*
 * 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();
}