(Latest Revision:
Nov 14, 2022)
Sort and Count Problem
EXAMPLE:
Consider the Sort-and-Count algorithm explained in section 5.3 of our text: "Counting Inversions"
Suppose that the initial list
is:92 71 36 91 27 48 14 34 81 26 24 65 78 51 37 22
Sort-and-Count makes two recursive calls.
The first recursive call inputs the first half of the initial
list (call it L): 92 71 36 91 27 48 14 34
The algorithm recursively sorts L, and returns a sorted copy of L, along with the number of inversions found in L (22).
The second recursive call inputs the second half of the initial
list (call it R): 81 26 24 65 78 51 37 22
Then the algorithm recursively sorts R, and returns a sorted copy of R, along with the number of inversions found in R (19).
Sort-and-Count then calls Merge-and-Count. To Merge-and-Count, Sort-and-Count
passes the sorted versions of L and R:14 27 34 36 48 71 91 92, and
22 24 26 37 51 65 78 81
Merge-and-Count begins merging L and R together, while counting the
inversions that exist between L and R.14 27 34 36 48 71 91 92 22 24 26 37 51 65 78 81
14 22 24 26 27 34 36 37 48 51 65 71 78 81 91 92
7 7 7 4 3 3 2 2 (inversions)
Shown on the line just above are the individual numbers (7 7 7 4 3 3 2 2) of inversions counted by Merge-and-Count as it places each element of R into the merged list. Each individual inversion count is displayed directly under the element of R to which it corresponds. Summing the individual L-R inversion counts, we get:
7+7+7+4+3+3+2+2 = 35.
Therefore the total number of inversions in the original list is the sum of the numbers of inversions found internal to L and R, plus 35, which is:
22+19+35 = 76.
YOUR ASSIGMENT:
Do the same work as above, except starting with this initial
list:71 30 89 62 84 31 10 92 70 55 42 72 44 48 93 77
Directions For Submitting Solution:
Here below is a sample of the information I want in your e-mail. This shows what the information would be for the example problem above. You must give me the same kind of information for your assigned problem.
Sorted Left Half: 14 27 34 36 48 71 91 92 (22 inversions found here)
Sorted Right Half: 22 24 26 37 51 65 78 81 (19 inversions found here)
Merge and Count Results:
Sorted Left Half-List Sorted Right Half-List
14 27 34 36 48 71 91 92 22 24 26 37 51 65 78 81
Merged Full List
14 22 24 26 27 34 36 37 48 51 65 71 78 81 91 92
7 7 7 4 3 3 2 2 (inversions counted)
Total Inversions Found in Final Merge: 7+7+7+4+3+3+2+2 = 35
Grand Total of Inversions in List: 22+19+35 = 76
- Prepare your solution to the problem, in the form indicated.
- COPY AND PASTE the text of your solution into an e-mail.
- DO NOT attach a file to your e-mail.
- DO copy and paste the text of your solution into the message window of the e-mail.
- DO Make the following exact text the subject line of your e-mail.
CS 4440 Sort+Count Problem
- DO copy the exact text above and & PASTE IT IN as the subject line of your e-mail
to make sure it is correct.
- Get it right, or get no credit!
- Send the E-mail to tester2@cs.csustan.edu