two dimensional array sorting in C/C++

This is a discussion on two dimensional array sorting in C/C++ within the C Programming forums, part of the General Programming Boards category; 1. Salem's compare() function does most of the work: it sorts with respect to the second column only. If you ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    1. Salem's compare() function does most of the work: it sorts with respect to the second column only. If you write a compare function that sorts in a "first column, second column way" than qsort() will work for you as well. You can also write a function that checks whether an array is already sorted by looping through it and returning an appropriate value before you start the algorithm.

    2, int (*a)[2] is a pointer to an array of two integers, iirc.

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > that the data is already sorted in a 1st column
    But does knowing that actually help in any way?
    You can see the '1 6' result is nowhere near the rest of the '1 x' values.

    Like it you have
    1 10
    2 9
    3 8
    4 7
    5 6
    6 5
    Then the whole array ends up being reversed!
    Yes, your example suggests something, but my counter example suggests something else.

    Maybe you can exploit the existing order in some way, but that's gonna take weeks or months of research with no obvious sign of success at the end of it.
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  4. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 07:15 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

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