// Select sort // The way it's done here is that it creates an entirely new list (sorted) which it then begins to fill with the // nodes from the original list. public void SelectSort( Comparison< Drug > UsersDrugComparer ) { //Create new "sorted" list DrugList sorted = new DrugList( ); //While there are still nodes in the original list... while( this.count > 0 ) { Node previous, minimum, removed; //Find the smallest node in the original list using the comparison method and return it and the one before it //NOTE: The comparison method in this case (found in pa4Test.cs) will actually sort it in descending order, but //this can be easily changed by removing the negative in the return statement in that method this.FindMinimalNode( out previous, out minimum, UsersDrugComparer ); //Remove that node and store it in the variable "removed" removed = this.RemoveNode( previous, minimum ); //Insert that node at the end of the sorted list sorted.InsertNode( removed, sorted.tail, null ); } this.count = sorted.count; this.tail = sorted.tail; this.head = sorted.head; }
// Insert sort // Similar to the selection sort above, this creates an entirely new list and fills it with the nodes from the original list public void InsertSort( Comparison< Drug > UsersDrugComparer ) { //Create new "sorted" list DrugList sorted = new DrugList( ); //While there are still nodes in the original list... while( this.count > 0 ) { Node previous, current, removed; //Remove the head node from the original list and store it in the variable "removed" removed = this.RemoveNode( null, this.head ); //Find the first node larger than "removed" in the sorted list using the comparison method and return it and the one before it sorted.FindFirstLargerNode( removed, out previous, out current, UsersDrugComparer ); //Insert "removed" in the correct position in the sorted list sorted.InsertNode( removed, previous, current ); } this.count = sorted.count; this.tail = sorted.tail; this.head = sorted.head; }