An algorithm prints the following pattern:
* * * * * * * * * * * * * * *
SOLUTION
A spreadsheet keeps track of student scores on all the exams in a course. Each row of the spreadsheet corresponds to one student, and each column in a row corresponds to his/her score on one of the exams. There are r students and c exams, so the spreadsheet has r rows and c columns.
Consider an algorithm that computes the total score on all exams for each student, and the average class score on each exam. You need to analyze the running time of this algorithm.
SOLUTION
Each exam average will added to r times. Since there are c exams, this gives another c*r additions.
Each exam average will be divided exactly once by the number of students, c. So, c divisions.
A card game program keeps a deck of cards in an array. Give an algorithm to "unshuffle" the deck so that all the cards of a suit are grouped together, and within each suit, the cards are in ascending order or rank--consider the ace as the lowest ranked card. Since there are only 52 cards, space is not an issue so you can use extra space if needed. The only constraint is that your algorithm be as fast as possible.
What is the worst case big O running time of your algorithm? What are the basic operations you used in your analysis? Is the average big O running time different from the worst case?
SOLUTION
Allocate four new arrays, each of length 13, one for each suit. Go through the original deck from front to end, and slot each card in its appropriate position in the suit array to which it belongs. So for example the queen of hearts will go to index position 11 in the hearts array. Note that since the indexes of the arrays are from 0 through 12, the array position of a card will be one less than its face value. (Queen's face value is 12, so its array position will be 11.)
The big O running time is O(1)!!
The basic operations are (a) looking up a card in the deck array, and (b) writing it into a suit array. Each of these takes unit time. (Writing takes unit time because the suit array is directly indexed with the card's face value.) Since there are 52 look ups and 52 writes, the total number of basic operations, and therefore the total units of time, is 104. This is a constant, so the big O time is O(1).
Pay attention to this result, because what it says is that no matter how many actual operations you perform, because the input size is always the same, 52, the number of operations does not vary. Hence O(1).
Two people compare their favorite playlists for matching songs. The first playlist has n songs, and the second has m. Each is stored in an array, in no particular order.
SOLUTION
Basic operation is a comparison between a pair of songs.
1+2+3+...+min(m,n)comparisons, which is O(min(m,n)2)
There is a highway with n exists numbered 0 to n - 1. You are given a list of the distances between them. For instance:
Exits: 1 2 3 4 5 6 Distances: 5 3 4 2 8 6
The distance from the start of the highway to exit 1
is 5
, from exit 1
to 2
is
3
, and so on.
You have to devise an algorithm that would calculate the distance between any two exits. Imagine that this distance function is called millions of times by applications that need this information, so you need to make your algorithm as fast as possible.
Describe your algorithm. What is the worst-case big O running time? Make sure to state all the parameters you use in the analysis and relate them to the algorithm.
SOLUTION
As the function that calculates the distance between any two exits is going to be called millions of times, it should be as fast as possible, ideally O(1) We can achieve this speed by creating an array, S of distances of each exit from the start of the highway. Assume that the original distances between exits (as in the example given in the problem) are in an array D
// copy distances between pairs of exits from D to S for (i=0; i < n; i++) { S[i] = D[i]; } // compute distance ("sum") from start for each exit for (i=1; i < n; i++) { S[i] = S[i-1] + S[i]; } D: 5 3 4 2 8 6 S: 5 8 12 14 22 28Creating the array takes linear time (O(n)) but using it will take constant time (O(1)). Once S is created, finding the distance from exit i to exit j is a simple matter of computing S[j] - S[i].
Running time Analysis