The definition of the class Heap is found in F&T p 690 and the core section of the HeapSort code is found on p 703 with other methods on previous pages.
The QuickSort code is found on pg 791 in F&T. This book implements the Quicksort algorithm in an "integrated" fashion in this sense. The code that many books separate into an independent "split" procedure has been included into the body of the Quicksort code.
The "split" section of the code works this way:
In each of the three categories, the left column refers to a list of 256 items, while the right column refers to a list of 512 items.
(It is an interesting exercise to approximate the difference in time between the two numbers, based on the fact that the second refers to a list that is double the first.)
It should be noted from examining the data that
| Sorting Method | Ordered | Random | Inversely Ordered | |||
|---|---|---|---|---|---|---|
| Straight insertion | 12 | 23 | 366 | 1444 | 704 | 2836 |
| Binary insertion | 56 | 125 | 373 | 1327 | 662 | 2490 |
| Straight selection | 489 | 1907 | 509 | 1956 | 695 | 2675 |
| Bubblesort | 540 | 2165 | 1026 | 4054 | 1492 | 5931 |
| Bubblesort w/flag | 5 | 8 | 1104 | 4270 | 1645 | 6542 |
| Shakersort | 5 | 9 | 961 | 3642 | 1619 | 6520 |
| Shellsort | 58 | 116 | 127 | 349 | 157 | 492 |
| Heapsort | 116 | 253 | 110 | 241 | 104 | 226 |
| Quicksort | 31 | 69 | 60 | 146 | 37 | 79 |
| Mergesort | 99 | 234 | 102 | 242 | 99 | 232 |
Noteworthy are the following points:
Supppose we have two arrays, both sorted, and we would like to combine them into another sorted array.
One option would be to copy one array to a third array, then copy the other array, then sort the new array from scratch using an efficient soring routine. This would be a straight-forward method, but it would ignore the fact that the original arrays were already sorted.
Instead, the common mergin algorihtm used is similar to this:
1) set pointers of all 3 arays to 1;
WHILE (more items still exist in BOTH input arrays) DO
| 2) compare lead items to find the smaller of
| the input arrays;
| 3) copy the smaller item to the merged array;
| 4) increment pointer of the merged array,
|_________ and of the appropriate input array;
5) copy rest of unused input arrays to merged array.
For example,
Array A: 1 2 5 6 8 9 Array B: 3 4 7 10 11 12
void mergarr(int a[], int b[], int c[],
int an, int bn, int & cn, int climit)
{ int aptr, bptr, cptr;
if(an+bn > climit)
{ cerr << "too many elements to be merged" << endl;
exit(1);
}
else {
cn = an + bn;
// initialize pointers
aptr = 0; bptr = 0; cptr = 0;
// major loop -- test to see if we have
// reached the end of one of the input arrays
while (aptr < an) && (bptr < bn){
if (a[aptr] < b[bptr])
{ c[cptr] = a[aptr];
aptr++; }
else
{ c[cptr] = b[bptr];
bptr++; }
cptr++;
}
// time to copy left-overs
// copy from a if a has anything left
while (aptr < an) {
c[cptr] = a[aptr];
cptr++; aptr++;
}
// else copy from b if b has anything left
while (bptr < bn) {
c[cptr] = b[bptr];
cptr++; bptr++;
}
}
}
This page is maintained by Dennis C. Smolarski, S.J.
© Copyright 1998, 1999 Dennis C. Smolarski, SJ, All rights reserved.
Last changed: 7 April 1999. Minor correction 9 June 1999