Exercises on Algorithmic Problem Solving Instructions: Make a “structured plan” to face the following situations to the best of your abilities (some exercises are already solved to serve as guide).
Focus on what have to be done, if the value is absent. What you have to do, is to create a new Set value for the absent entry.
Of course, if you use an operation which has an atomicity guaranty for the creation of the Set only, adding to the Set will happen concurrently, which requires using a concurrent Set. You can utilize a ConcurrentHashMap to create a de-facto ConcurrentHashSet (which doesn’t exist in that form) by mapping to a fixed value, which is especially simple if you let the value signalling presence be Boolean.TRUE: ConcurrentHashMap map=new ConcurrentHashMap; final BiConsumer action = (key, value) - map.computeIfAbsent(key, x-ConcurrentHashMap.newKeySet).addAll(value).
Exercise 2.3.2 Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once either array L or R has had all its elements copied back to A and then copying the remainder of the other array back into A. Here is a simple modification: MERGE(A, p, q, r) n1 = q - p + 1 n2 = r - q let L1.n₁ and R1.n₂ be new arrays for i = 1 to n₁ Li = Ap + i - 1 for j = 1 to n₂ Rj = Aq + j i = 1 j = 1 for k = p to r if i n₁ Ak = Rj j = j + 1 else if j n₂ Ak = Li i = i + 1 else if Li ≤ Rj Ak = Li i = i + 1 else Ak = Rj j = j + 1 C code.