Quick Sort Structs
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Quick Sort Structs

  1. #1
    Join Date
    Dec 2008
    Posts
    1

    Quick Sort Structs

    Hey i am new to c plus plus, and really need some help!
    Basically i have information, in a file, first name, second name and idnumber stored in a struct for a number of people as such:

    struct person {
    char idnumber [numbersize];
    char firstname [namesize];
    char secondname [namesize];

    };

    And i need to be able to sort it according to either first name, second name or in ascending idnumber using a quick sort. Is it possible to write a general function and use pointers to assign the relevant piece of information to sort by e.g first name etc?. Then, is it possible to call the function and print the sorted list?

    If someone could show me how to do this or offer some helpful code or some hints it would be greatly appreciated.
    Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Quick Sort Structs

    Quote Originally Posted by jaqueshoratio View Post
    Hey i am new to c plus plus, and really need some help!
    Basically i have information, in a file, first name, second name and idnumber stored in a struct for a number of people as such:

    struct person {
    char idnumber [numbersize];
    char firstname [namesize];
    char secondname [namesize];

    };

    And i need to be able to sort it according to either first name, second name or in ascending idnumber using a quick sort.
    Homework? There is a std::sort in C++ that is used for this purpose.
    Is it possible to write a general function and use pointers to assign the relevant piece of information to sort by e.g first name etc?. Then, is it possible to call the function and print the sorted list?
    If this is homework, then you can call a general function, and the general function calls a specialized compare function function that compares two elements if they are not in order.
    Code:
    #include <cstring>
    
    static const int numbersize = 50;
    static const int namesize = 50;
    
    struct person {
        char idnumber [numbersize];
        char firstname [namesize];
        char secondname [namesize];
    };
    
    typedef bool (*CompareFn)(person*,  person*);
    
    bool CompareByName(person* first, person* second)
    {
        return strcmp(first->firstname, second->firstname) < 0;
    }
    
    bool CompareByID( person* first, person* second)
    {
    // ... you fill this in.  return true if first comes
    // before second, false otherwise
       return true;
    }
    
    void QuickSort(person* firstperson, int nPersons, CompareFn fnCompare)
    {
           //...
           person *person1;
           person* person2;
           //...
           // swap part of quicksort  
           // if fnCompare returns false, then swap these two persons
           if ( !fnCompare( person1, person2 ) )
           {
              // swap them, since they're out of order
           }
            // rest of the quicksort code
    }
    
    int main()
    {
       person MyPerson[40];
       // fill in MyPerson with data
       //...
       // now call QuickSort to sort by name
       QuickSort( MyPerson, 40, CompareByName );
    
        // now call it to sort by ID
       QuickSort( MyPerson, 40, CompareByID );
    }
    You fill in the rest.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center