This is an open book, open notes, and open computer test. You may not use any books other than your textbook, and you may not use Internet resources from off campus. If you get stuck, talk to Jeff Ondich, but please don't talk to anyone else about the exam.
// precondition: N > 0 int MysteryFunction( int a[], int N ) { int k = a[0]; if( N > 1 ) { int tmp = MysteryFunction( a, N-1 ); if( a[N-1] > tmp ) k = a[N-1]; else k = tmp; } //cout << k << endl; return( k ); }
struct Node { int data; Node *next; }; // precondition: head points to a linked list. int EnigmaFunction( Node *head ) { int n = 0; Node *current = head; while( current != 0 ) { n++; current = current->next; } return( n ); }
Selection, Insertion, and Merge Sorts are all members of a class of sorting algorithms whose performance is usually measured by counting the number of array element comparisons need to be done to complete the sorting of the array. That is, we count the number of times something like "if( a[i] < a[j] )" gets executed during the run of the algorithm. (Note that in some algorithms, we've stashed a[i] into a temporary variable, so the comparison looks like "if( tmp < a[j] )". It would take several pages to provide a reasonably rigorous argument that the comparison count is a good measure of performance for these algorithms. With a lot less rigor, we can say something like this: by counting comparisons, we are counting the number of iterations of the inner loop, and by counting inner loop iterations, we are measuring the most time-consuming portion of the algorithm.
So, in what follows, you're going to count comparisons
and measure running times for the three algorithms and several
values of N. Let's get started.
To time a program, type "time programname" at the UNIX command line.
N | Comp. count for Sel. Sort | Running time for Sel. Sort | Comp. count for Ins. Sort | Running time for Ins. Sort | Comp. count for Merge Sort | Running time for Merge Sort |
---|---|---|---|---|---|---|
125 | . | . | . | . | . | . |
250 | . | . | . | . | . | . |
500 | . | . | . | . | . | . |
1000 | . | . | . | . | . | . |
10000 | . | . | . | . | . | . |
20000 | . | . | . | . | . | . |
30000 | . | . | . | . | . | . |