Name (print *CLEARLY*): _________________________________________________


Student ID (print *CLEARLY*; 1 digit per underline): _____ _____ _____ _____ _____ _____

CSE 231s Quiz 27: Suspect Merge Sort

For the merge sort studio, you were charged with sorting an array in place. Further, you were to sort it in parallel as long as a predicate passed the test with the current range length.
public interface Sorter {
	default void sort(T[] data, Comparator comparator) {
		throw new NotYetImplementedException();
	}
	void sortRange(T[] data, Comparator comparator, int min, int maxExclusive);
}

public class ParallelMergeSorter implements Sorter {
	private final Combiner combiner;
	private final IntPredicate isParallelDesired;
	private final Sorter sequentialSorter;
	public ParallelMergeSorter(Combiner combiner, IntPredicate isParallelDesired, Sorter sequentialSorter) {
		this.combiner = combiner;
		this.isParallelDesired = isParallelDesired;
		this.sequentialSorter = sequentialSorter;
	}
	public ParallelMergeSorter(Combiner combiner, IntPredicate isParallelDesired) {
		this(combiner, isParallelDesired, new SequentialMergeSorter<>(combiner.getComponentType()));
	}
	@Override
	public void sortRange(T[] data, Comparator comparator, int min, int maxExclusive) {
		throw new NotYetImplementedException();
	}
}

1a) What is the most interesting piece data? __________________________________
1b) Will it be shared? Yes / No
1c) If so, how broadly will it be shared? ___________________________________________________
1d) Is it mutable? Yes / No
1e) If so, are we actually going to mutating it? Yes / No
1f) If it is shared and mutated, what is the plan to keep our solution thread safe?

 


 


 

2) There is a bug in the code below. Where is it?

@Override
public void sortRange(T[] data, Comparator comparator, int min, int maxExclusive) {
	int rangeLength = (maxExclusive - min);
	if (rangeLength > 1) {
		if (isParallelDesired.test(rangeLength)) {
			int mid = (maxExclusive + min) / 2;
			finish(() -> {
				async(() -> {
					sortRange(data, comparator, min, mid);
				});
				sortRange(data, comparator, mid, maxExclusive);
			});
			combiner.combineRange(data, comparator, min, mid, maxExclusive);
		} else {
			sequentialSorter.sort(data, comparator);
		}
	}
}