Thread: two dimensional array sorting in C/C++

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

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, 08:15 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM