I've been trying to understand the non-recursive MergeSort algorithm after recently coding the recursive version. My AP book doesn't provide too much info or examples on this topic so I was hoping someone could help me clear things up a bit.
What does my book mean by the following: "In a non-recursive mergeSort method. We divide the list into two equal-sized parts and sort each with the selection sort, then merge the two using an algorithm to be discussed in part B."
Does one always divide an array into two pieces in a non-recursive mergeSort method (and then sort them accordingly), or are there cases like the recursive version where you keep dividing until you get to array.length of 2 or 1?
Book code:
void mergeSort (ArrayList <Integer> A, int first, int last){
int mid;
mid = (first + last) / 2;
selectionSort (A, first, mid);
selectionSort (A, mid+1, last);
merge (A, first, mid, last);
}
If you always divide the array into 2 and then sort how is this efficient? What happens if your array contains thousands of values . . . wouldn't recursive be better off as it sorts the values in smaller pieces?
Book demonstration: