I am trying to perform a selection sort on a linked list using recursion and I am having some trouble partitioning my linked list around the node with the smallest value on each pass through my recursive sort function. I am trying to get the node with the smallest value, partition the linked list around the smallest value, append the smallest to the front, join the two partitioned lists, and then perform the sort again on the joined partitioned list until the entire linked list is sorted. For example:
q w e r t // partition around e
e -> q w r t // join the partitions
eq -> w r t // append q to e
eq -> w r t // partition around r
and so forth.
My sort method:
Node.prototype.sort = function(){
if(!next){
return this;
} else {
var a = null;
var b = null;
var smallest = this.smallest();
splitIt(smallest, a, b);
appendSmallest(smallest);
a.join(b);
a.sort();
}
}
I get the smallest node like so:
Node.prototype.smallest = function(){
if(!next) return this;
var sm = next.smallest();
if(sm.data < this.data){
return sm;
}
return this;
}
Here are my append and join methods:
Node.prototype.appendSmallest = function(smallest){
if(!next) next = smallest;
}
Node.prototype.join = function(otherNode){
if(!next) next = otherNode;
else next.join(otherNode);
}
I am having some trouble implementing the splitIt
method recursively. What would the pseudocode be for such operation?