Reputation: 5054
Bottom up merge sort treats a list each element of size 1, and iteratively merge the sub-lists back and forth until they are in sorted order. This makes it very attractive to use bottom up merge sort to sort linked list as they are already "separated".
I'm trying to implement bottom up merge sort in C language, and realised that there are various approach to implement bottom up merge sort; in particular, I'm using this approach:
However, I'm struggling to implement the following:
Hence, my questions are:
I suppose this is one of the advantage of using bottom-up merge sort on linked list, as it is more space efficient than using bottom-up merge sort on arrays. Correct me if I'm wrong.
Upvotes: 1
Views: 2461
Reputation: 28828
There's a clever method that uses a fixed size array (26 or 32 are common sizes) array of pointers to nodes, some working local pointers to nodes, and a standard mergelists() function that merges two already sorted lists, and needs to handle empty lists to make the main code simpler. Nodes are merged into the array, then the array is merged into a single sorted list. The first part of this logic is:
// initialize array[] to all NULLs
while(plist != NULL){
pmerge = plist;
plist = plist->next;
pmerge->next = NULL;
for(i = 0; i < array size; i++){
if(array[i] == NULL)
break;
pmerge = mergelists(array[i], pmerge);
array[i] = NULL;
}
if(i == array size)i--;
array[i] = pmerge;
}
The size of the list doubles as i increases, array[i] is either NULL or points to a list with 2 to the power i nodes.
Then the array is merged into a single list, again with just a loop and a call to mergelists(). See if you can figure out this part.
Upvotes: 1
Reputation: 9570
Upvotes: 2