// 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;
 }
Exemple #2
0
    static void Main( )
    {
        DrugList drugs = new DrugList("RXQT1402.txt");

        drugs.InsertSort( );
        foreach (Drug d in drugs.ToArray( ))
        {
            Console.WriteLine(d);
        }
    }
 static void Main( )
 {
     const string drugFileName = "RXQT1503.txt";
     
     // Read the drugs from the file into a linked list.
     DrugList drugsList = new DrugList( );
     using( StreamReader sr = new StreamReader( drugFileName ) )
         while( ! sr.EndOfStream ) drugsList.Append( Drug.Parse( sr.ReadLine( ) ) );
         
     // Sort the list by total amount paid for each drug.
     Console.WriteLine( );
     Console.WriteLine( "Top 10 drugs by total amount paid:" );
     drugsList.SelectSort( CompareByTotalPaidDecreasing );
     {
         int count = 0;
         foreach( Drug d in drugsList.Enumeration )
         {
             count ++;
             Console.WriteLine( "{0:d2}: {1,10:n0} - {2}", count, d.TotalPaid, d.Name );
             if( count == 10 ) break;
         }
     }
         
     // Sort the list by number of units dispensed for each drug.
     Console.WriteLine( );
     Console.WriteLine( "Top 10 drugs by number of units dispensed:" );
     drugsList.InsertSort( CompareByQuantityDecreasing );
     {
         int count = 0;
         foreach( Drug d in drugsList.Enumeration )
         {
             count ++;
             Console.WriteLine( "{0:d2}: {1,10:n0} - {2}", count, d.Quantity, d.Name );
             if( count == 10 ) break;
         }
     }
 }
 // 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;
 }
Exemple #5
0
 static void Main( )
 {
     DrugList drugs = new DrugList( "RXQT1402.txt" );
     drugs.InsertSort( CompareByName );
     foreach( Drug d in drugs.ToArray( TestByName ) ) Console.WriteLine( d );
 }
Exemple #6
0
    //========================================================================
    // Methods which sort the list:
    // SelectSort: Given a Func< Drug, Drug, int > comparison method, the SelectSort
    // method uses repeated calls to RemoveMin and Append to convert the linked list
    // to a sorted linked list ordered by the comparison method completed, not
    //tested
    public void SelectSort( Func< Drug, Drug, int > userCompare)
    {
        Node current = head;
        int count = 0;

        //counting the number of elements in the list
        while(current != null)
        {
            current = current.Next;
            count++;
        }

        //creating a new empty list
        DrugList temp = new DrugList();

        Drug temporary = RemoveMin(userCompare);
        // removing the first minimum value from the list
        //and making a copy of it

        while (0 <= count)
        {
            // adding the head to the temp list
            temp.Append(temporary);
            temporary = RemoveMin(userCompare);

            // if there's nothing left in the list we're sorting from
            if (temporary == null)
            {
                break;
            }

            count--;
        }

        head =  temp.head;
        tail = temp.tail;
    }
Exemple #7
0
    // Sort this list by insertion sort with a user-specified comparison method.
    // InsertSort: Given a Func< Drug, Drug, int > comparison method, the InsertSort
    // method uses repeated callsto RemoveFirst and InsertInOrder to convert the
    //linked list to a sorted linked list ordered by the comparison method.
    public void InsertSort( Func< Drug, Drug, int > userCompare)
    {
        DrugList temp = new DrugList();

        Drug tempCompare = RemoveFirst();
        while(tempCompare != null)
        {
            temp.InsertInOrder(tempCompare, userCompare);
            tempCompare = RemoveFirst();
        }
        head = temp.head;
        tail = temp.tail;
    }