Suppose we are doing selection and merge sorts on the list below, and that we are operating on a computer where essentially every operation is cost-free (takes 0 time to execute) except for the act of assigning a value into a slot of the array passed in (such as setting a1[0] = 42;
), which requires 100 units of time.
(Assigning a value to a normal int
variable, or into any other array or collection, requires no time in this hypothetical model.)
How long does each algorithm take to run for this particular data under these particular conditions?
Which sorting algorithm requires less time to run?
Assume that selection does not perform a swap if not necessary, that is, if the two indexes of interest are the same.
// index 0 1 2 3 4 5 6 7
int[] a1 = {15, 56, 24, 5, 39, -4, 27, 10};
selectionSort(a1);
int[] a2 = {15, 56, 24, 5, 39, -4, 27, 10};
mergeSort(a2);