Sorting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Speed

    #1

    Sorting

    Hi,

    I have an array that I want to sort without losing the index
    information.

    For eg. sorting X[5] = {34.5, 56.4, 12.9, 43.2, 21.7};

    should return {2, 4, 0, 3, 1}

    Could you please tell me how to do it quickly.

    Many thanks,
    Speed.
  • Eric Sosman

    #2
    Re: Sorting

    Eric Sosman wrote:
    [...]
    Solution #2: Leave X[] alone and instead sort an array of
    indices to it:
    >
    double X[] = { 34.5, 56.4, 12.9, ... };
    double I[] = { 0, 1, 2, ... };
    [...]
    Oh, rats. `int I[]', of course, or even `size_t I[]'
    if you anticipate sorting large arrays. Sorry about that.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Willem

      #3
      Re: Sorting

      Speed wrote:
      ) I have an array that I want to sort without losing the index
      ) information.
      )
      ) For eg. sorting X[5] = {34.5, 56.4, 12.9, 43.2, 21.7};
      )
      ) should return {2, 4, 0, 3, 1}
      )
      ) Could you please tell me how to do it quickly.

      Start with {0,1,2,3,4}. Sort this, using X[a] <=X[b] as operator.

      The qsort() library function is usually your best bet.


      NB: The <=operator is not C. I use it as shorthand for:
      X[a] < X[b] ? -1 : X[a] X[b]


      SaSW, Willem
      --
      Disclaimer: I am in no way responsible for any of the statements
      made in the above text. For all I know I might be
      drugged or something..
      No I'm not paranoid. You all think I'm paranoid, don't you !
      #EOT

      Comment

      • Speed

        #4
        Re: Sorting

        On Feb 23, 8:03 pm, Willem <wil...@stack.n lwrote:
        Speed wrote:
        >
        ) I have an array that I want to sort without losing the index
        ) information.
        )
        ) For eg. sorting X[5] = {34.5, 56.4, 12.9, 43.2, 21.7};
        )
        ) should return {2, 4, 0, 3, 1}
        )
        ) Could you please tell me how to do it quickly.
        >
        Start with {0,1,2,3,4}. Sort this, using X[a] <=X[b] as operator.
        >
        The qsort() library function is usually your best bet.
        >
        NB: The <=operator is not C. I use it as shorthand for:
        X[a] < X[b] ? -1 : X[a] X[b]
        >
        SaSW, Willem
        --
        Disclaimer: I am in no way responsible for any of the statements
        made in the above text. For all I know I might be
        drugged or something..
        No I'm not paranoid. You all think I'm paranoid, don't you !
        #EOT
        Hi,

        Many thanks for the replies. I am not quite sure how to use qsort() to
        sort the array of indices. Could you please tell me how to call with
        indices?

        Many thanks,
        Speed.

        Comment

        • Willem

          #5
          Re: Sorting

          Speed wrote:
          ) Many thanks for the replies. I am not quite sure how to use qsort() to
          ) sort the array of indices. Could you please tell me how to call with
          ) indices?

          I don't know the details of what you're trying to achieve, so how about
          you post whatever you think looks like a reasonable piece of code that
          tries to do what you want, and then some of the people on this newsgroup
          will undoubtedly help out with the bits that aren't working for you.


          SaSW, Willem
          --
          Disclaimer: I am in no way responsible for any of the statements
          made in the above text. For all I know I might be
          drugged or something..
          No I'm not paranoid. You all think I'm paranoid, don't you !
          #EOT

          Comment

          • Richard Heathfield

            #6
            Re: Sorting

            Willem said:
            Richard wrote:
            Please retain my surname when quoting, Willem - not because I'm precious
            about it or anything, but simply to disambiguate me from the Riley troll.
            Thanks.
            )
            ) Um, why not just sort pointers?
            >
            Good idea, although that doubles the amount of data that qsort
            has to shuffle. But maybe we can do without. How about:
            Yes, as soon as you said "although that doubles", I thought of a way to do
            without. (Yes, same way that you thought of, although it took me a little
            longer!)

            <snip>
            Although that's still a lot of extra overhead just to avoid
            a global pointer.
            <shrugIt's a point of view. Some people would see the overhead as
            insignificant compared to the maintainability issues of file scope
            objects.

            I do wish qsort allowed a void *extra, though.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Antoninus Twink

              #7
              Re: Sorting

              On 25 Feb 2008 at 19:23, Richard Heathfield wrote:
              Willem said:
              >
              >Richard wrote:
              >
              Please retain my surname when quoting, Willem - not because I'm precious
              about it or anything,
              You? Precious about something? Unthinkable.
              but simply to disambiguate me from the Riley troll.
              Ha! I can't imagine *he'd* be too pleased to be mixed up with *you* ...


              Comment

              Working...