Optimizing struct sorting algorithm

This is a discussion on Optimizing struct sorting algorithm within the C++ Programming forums, part of the General Programming Boards category; I want to sort 4 players according to their scores (player[0].win through player[3].win) and then display the sorted list as ...

  1. #1
    843
    843 is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,750
    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...)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    843
    843 is offline
    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, 01:24 PM
  3. Replies: 1
    Last Post: 12-03-2008, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21