Assume that the array of tuples [{7, 4 }, {2, 1 }, {8, 0 }, {12, 1 }, {0, 5 }, {
ID: 3889635 • Letter: A
Question
Assume that the array of tuples [{7, 4}, {2, 1}, {8, 0}, {12, 1}, {0, 5}, {3, 4}, {8, 6}, {3, 1}] will be sorted, with a stable sorting method and that it will be sorted based on the second number (written in bold font) in the tuple. Write the sorted array.
In order to match my answer exactly (and be graded as correct by the auto-grading tool), make sure you write the square brackets for the array, the curly braces ({}) and the comma (,) for each tuple and that you DO NOT leave any spaces anywhere. For example, the above array would be written: [{7,4},{2,1},{8,0},{12,1},{0,5},{3,4},{8,6},{3,1}]
If you do not match the answer because you did not follow these instructions, you will not get the points.
Explanation / Answer
Consider only the 2nd element of the tuple
4,1,0,1,5,4,6,1
Assuming that the elements with the same value for second number will be sorted according to their position in the original tuple array, we will do the sort.
Please make note of the assumptionso 2nd element will get sorted as
0,1,1,1,4,4,5,6
Tuple will get sorted as:
8,0
2,1
12,1
3,1
7,4
3,4
0,5
8,6
So the answer in correct form will be:
[{8,0},{2,1},{12,1},{3,1},{7,4},{3,4},{0,5},{8,6}]