Algorithms Homework 4 Merge Sort and Quicksort
Algorithms Homework 4 Merge Sort and Quicksort
Unit 12 Algorithms
Merge Sort
(b) Describe briefly how the algorithm works. [5]
-Split the list down in half repeatedly, until the lists are single pieces of data
-Reconstruct the lists by comparing each term in the list one by one to reconstruct in the correct
order
-Keep rejoining the lists in correct order until all of the lists are merged
(c) Which of the following is the order of time complexity of the algorithm?
O(n log n) [1]
2. (a) Explain the function and significance of the “split point” in the quicksort algorithm. [2]
This is the number which splits the lists, into upper and lower lists
(b) An incomplete algorithm for the quicksort is given on the next page.
(i) Complete the algorithm by inserting the missing lines at A, B, C, D and E. [9]
(ii) What is the initial value of the pivot in the pseudocode below? 9 [1]
1
Homework 4 Merge sort and quick sort
Unit 12 Algorithms
(iii) What is the base case that stops the recursion from continuing indefinitely? [1]
Total 20 marks If start is less than end, this means that if there is only one item in
the list then it will not run, or if the list is two items then and slrreayd sorted it will
not run.
2
Homework 4 Merge sort and quick sort
Unit 12 Algorithms
leftmark += 1
endwhile
while alist[rightmark] >= pivot and rightmark >= leftmark
rightmark -= 1
endwhile
if rightmark < leftmark
done = True
else
# swap the list items
Temp = alist[leftmark]
Alist[leftmark] = alist[rightmark]
Alist[rightmark] = temp
endif
endwhile
# swap the pivot with alist[rightmark]
Alist[start] = alist[rightmark]
Alist[rightmark] = pivot
return rightmark
endfunction partition
3
Homework 4 Merge sort and quick sort
Unit 12 Algorithms
endfunction quicksort