Thread: Optimizing struct sorting algorithm

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Optimizing struct sorting algorithm

    I want to sort 4 players according to their scores (player[0].win through player[3].win) and then display the sorted list as such: A B C D.

    What would be an efficient algorithm to solve this problem? The one I'm using is very tedious and takes a lot of space. Here's a quarter of the code snippet:

    Code:
        *pointer =  max(max(player[0].pts, player[1].pts),
                    max(player[2].pts, player[3].pts));
    
        if (*pointer == player[0].pts) {
            pos1 = 'A';
            *pointer = max( player[1].pts,
                            max(player[2].pts, player[3].pts));
    
            if (*pointer == player[1].pts) {
                pos2 = 'B';
                *pointer = max( player[2].pts, player[3].pts);
    
                if (*pointer == player[2].pts) {
                    pos3 = 'C';
                    pos4 = 'D';
                }
    
                else {
                    pos3 = 'D';
                    pos4 = 'C';
                };
            }
    
            else if (*pointer == player[2].pts) {
                pos2 = 'C';
                *pointer = max( player[1].pts, player[3].pts);
    
                if (*pointer == player[1].pts) {
                    pos3 = 'B';
                    pos4 = 'D';
                }
    
                else {
                    pos3 = 'D';
                    pos4 = 'B';
                };
            }
    
            else if (*pointer == player[3].pts) {
                pos2 = 'D';
                *pointer = max( player[1].pts, player[2].pts);
    
                if (*pointer == player[1].pts) {
                    pos3 = 'B';
                    pos4 = 'C';
                }
    
                else {
                    pos3 = 'C';
                    pos4 = 'B';
                };
            }
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    There are standard functions available in C and C++ for sorting an array of things.

    For example
    qsort(3): sorts array - Linux man page

    All you need to do is write a function which takes two "pointers to elements" which can return a result indicating what "order" the two elements should be in.

    There are plenty of examples of using qsort on this board (I've posted plenty).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use std::sort with the same predicate that you wrote for std::max_element based on my example. (But I notice that you're not using max_element...)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks for the info. I didn't use max_elements because I figured it would be easier to use max() for finding the 2nd and 3rd element of the array. The max_element seems to be useful only for finding the top value. Well, either that or I'm just having a hard time trying to program it.
    Last edited by 843; 04-08-2011 at 01:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 10:12 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM