Using your code template from Lesson 17 as a starting point, write a recursive mergeSort
method as described in the student lesson. Pseudocode for the recursive mergeSort
method is given below.
// Recursively divides a list in half, over and over. When the
// sublist has one or two values, stop subdividing.
void mergeSort(ArrayList <Comparable> a, int first, int last) {
if (sublist has only one value) {
// do nothing
} else if (sublist has two values) {
// sort it if necessary
} else { // recursion, divide list into two halves
// Find midpoint of current sublist
// Call mergeSort and process left sublist
// Call mergeSort and process right sublist
// merge left and right sublists
}
}
Note that you will have to rewrite a new
merge
to fit the necessary calls of the newmergeSort
method.
For both mergeSort() and merge() you must work with the single ArrayList parameter that is passed in. (i.e. You may not modify the merge() method to accept more ArrayLists.) However, you are free to create a temporary list within the merge method.
If you want an extra challenge, try writing the merge() method without using a temporary list. This is possible with ArrayLists.
Complete the merge and mergeSort methods within your code template from Lesson 17.
Add the necessary code to count steps. Note that since mergeSort calls merge, you will need to include step counting there as well.
Repeat steps 5 to 7 from Lab 17.4, including mergeSort in your graphs.
100 integers | 200 integers | 400 integers | 800 integers | |
---|---|---|---|---|
BubbleSort | ___ | ___ | ___ | ___ |
SelectionSort | ___ | ___ | ___ | ___ |
InsertionSort | ___ | ___ | ___ | ___ |
MergeSort | ___ | ___ | ___ | ___ |
PX_LastName_FirstName_Sorts.java
code below that includes recursive mergeSort and merge.You must Sign In to submit to this assignment
Last modified: January 23, 2023
Back to Lab 18.1 MergeDark Mode
Outline