how to sort parallel arrays in c++?

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

    how to sort parallel arrays in c++?

    say, if you had parallel arrays containing the sales person id, the month, and
    the sales figures (for that person in that month), sorting on sales figure and
    preserve the order of figures within the data about the same person:

    Original Data:
    id month sales
    +-----+ +-----+ +-----+
    | 432 | | 8 | | 89 |
    +-----+ +-----+ +-----+
    | 123 | | 8 | | 116 |
    +-----+ +-----+ +-----+
    | 555 | | 8 | | 203 |
    +-----+ +-----+ +-----+
    | 432 | | 9 | | 105 |
    +-----+ +-----+ +-----+
    | 123 | | 9 | | 143 |
    +-----+ +-----+ +-----+
    | 555 | | 9 | | 187 |
    +-----+ +-----+ +-----+

    Sorted by sales:
    id month sales
    +-----+ +-----+ +-----+
    | 432 | | 8 | | 89 |
    +-----+ +-----+ +-----+
    | 432 | | 9 | | 105 |
    +-----+ +-----+ +-----+
    | 123 | | 8 | | 116 |
    +-----+ +-----+ +-----+
    | 123 | | 9 | | 143 |
    +-----+ +-----+ +-----+
    | 555 | | 9 | | 187 |
    +-----+ +-----+ +-----+
    | 555 | | 8 | | 203 |
    +-----+ +-----+ +-----+

    is that possible to do this in c++? Thanks!
  • Victor Bazarov

    #2
    Re: how to sort parallel arrays in c++?

    Xiaozhu wrote:[color=blue]
    > say, if you had parallel arrays containing the sales person id, the month, and
    > the sales figures (for that person in that month), sorting on sales figure and
    > preserve the order of figures within the data about the same person:
    >
    > Original Data:
    > id month sales
    > +-----+ +-----+ +-----+
    > | 432 | | 8 | | 89 |
    > +-----+ +-----+ +-----+
    > | 123 | | 8 | | 116 |
    > +-----+ +-----+ +-----+
    > | 555 | | 8 | | 203 |
    > +-----+ +-----+ +-----+
    > | 432 | | 9 | | 105 |
    > +-----+ +-----+ +-----+
    > | 123 | | 9 | | 143 |
    > +-----+ +-----+ +-----+
    > | 555 | | 9 | | 187 |
    > +-----+ +-----+ +-----+
    >
    > Sorted by sales:
    > id month sales
    > +-----+ +-----+ +-----+
    > | 432 | | 8 | | 89 |
    > +-----+ +-----+ +-----+
    > | 432 | | 9 | | 105 |
    > +-----+ +-----+ +-----+
    > | 123 | | 8 | | 116 |
    > +-----+ +-----+ +-----+
    > | 123 | | 9 | | 143 |
    > +-----+ +-----+ +-----+
    > | 555 | | 9 | | 187 |
    > +-----+ +-----+ +-----+
    > | 555 | | 8 | | 203 |
    > +-----+ +-----+ +-----+
    >
    > is that possible to do this in c++? Thanks![/color]

    Your problem is not defined enough. There is ambiguity. What
    should happen if the initial data are

    +-----+ +-----+ +-----+
    | 432 | | 8 | | 89 |
    +-----+ +-----+ +-----+
    | 123 | | 8 | | 116 |
    +-----+ +-----+ +-----+
    | 555 | | 8 | | 203 |
    +-----+ +-----+ +-----+
    | 432 | | 9 | | 105 |
    +-----+ +-----+ +-----+
    | 123 | | 9 | | 103 | ** different than yours
    +-----+ +-----+ +-----+
    | 555 | | 9 | | 187 |
    +-----+ +-----+ +-----+

    ?

    My answer is this: I'd not have "parallel arrays". I'd put all
    the values in a structure

    struct salesdata {
    int id;
    int month;
    int value;
    };

    and sorted the array of these structs based on some criteria.

    Victor

    Comment

    • osmium

      #3
      Re: how to sort parallel arrays in c++?

      Xiaozhu writes:
      [color=blue]
      > say, if you had parallel arrays containing the sales person id, the month,[/color]
      and[color=blue]
      > the sales figures (for that person in that month), sorting on sales figure[/color]
      and[color=blue]
      > preserve the order of figures within the data about the same person:[/color]

      That's a severe problem with parallel arrays and is one of the many reasons
      you need structures.


      Comment

      • Alf P. Steinbach

        #4
        Re: how to sort parallel arrays in c++?

        * Xiaozhu:[color=blue]
        > say, if you had parallel arrays containing the sales person id, the month, and
        > the sales figures (for that person in that month), sorting on sales figure and
        > preserve the order of figures within the data about the same person:
        >
        > Original Data:
        > id month sales
        > +-----+ +-----+ +-----+
        > | 432 | | 8 | | 89 |
        > +-----+ +-----+ +-----+
        > | 123 | | 8 | | 116 |
        > +-----+ +-----+ +-----+
        > | 555 | | 8 | | 203 |
        > +-----+ +-----+ +-----+
        > | 432 | | 9 | | 105 |
        > +-----+ +-----+ +-----+
        > | 123 | | 9 | | 143 |
        > +-----+ +-----+ +-----+
        > | 555 | | 9 | | 187 |
        > +-----+ +-----+ +-----+
        >
        > Sorted by sales:
        > id month sales
        > +-----+ +-----+ +-----+
        > | 432 | | 8 | | 89 |
        > +-----+ +-----+ +-----+
        > | 432 | | 9 | | 105 |
        > +-----+ +-----+ +-----+
        > | 123 | | 8 | | 116 |
        > +-----+ +-----+ +-----+
        > | 123 | | 9 | | 143 |
        > +-----+ +-----+ +-----+
        > | 555 | | 9 | | 187 |
        > +-----+ +-----+ +-----+
        > | 555 | | 8 | | 203 |
        > +-----+ +-----+ +-----+
        >
        > is that possible to do this in c++? Thanks![/color]

        Well the problem has nothing to do with parallell arrays (because you can
        always just sort an array of indices to the real arrays), but it has to do
        with availability of a _stable_ sort, one which preserves the order of
        records with equal keys.

        You can implement a stable sort yourself, but I gather the question is
        whether the built-in sorting algorithms in the standard library are stable.

        list::sort is documented as stable in §23.2.2.4/31, and also <algorithm>
        has a stable sort called, suprise!, std::stable_sor t (§25.3.1.2).

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Mabden

          #5
          Re: how to sort parallel arrays in c++?

          "Xiaozhu" <gee.ygary3@gma il.com> wrote in message
          news:f8c34951.0 408310853.20c26 6a1@posting.goo gle.com...[color=blue]
          > say, if you had parallel arrays containing the sales person id, the month,[/color]
          and[color=blue]
          > the sales figures (for that person in that month), sorting on sales figure[/color]
          and[color=blue]
          > preserve the order of figures within the data about the same person:
          >
          > Original Data:
          > id month sales
          > +-----+ +-----+ +-----+
          > | 432 | | 8 | | 89 |
          > +-----+ +-----+ +-----+
          > | 123 | | 8 | | 116 |
          > +-----+ +-----+ +-----+
          > | 555 | | 8 | | 203 |
          > +-----+ +-----+ +-----+
          > | 432 | | 9 | | 105 |
          > +-----+ +-----+ +-----+
          > | 123 | | 9 | | 143 |
          > +-----+ +-----+ +-----+
          > | 555 | | 9 | | 187 |
          > +-----+ +-----+ +-----+
          >
          > Sorted by sales:
          > id month sales
          > +-----+ +-----+ +-----+
          > | 432 | | 8 | | 89 |
          > +-----+ +-----+ +-----+
          > | 432 | | 9 | | 105 |
          > +-----+ +-----+ +-----+
          > | 123 | | 8 | | 116 |
          > +-----+ +-----+ +-----+
          > | 123 | | 9 | | 143 |
          > +-----+ +-----+ +-----+
          > | 555 | | 9 | | 187 |
          > +-----+ +-----+ +-----+
          > | 555 | | 8 | | 203 |
          > +-----+ +-----+ +-----+
          >
          > is that possible to do this in c++? Thanks![/color]

          Perhaps I'm missing something, but it seems to me a linked list of pointers
          would do what you want quite easily. Something like:

          struct StrSales {
          StrSales *next;
          int *id;
          int *month;
          int *sales;
          };

          This can then be made into a list that points to the original arrays so they
          are not changed, but you can sort them anyway you wish by stepping through
          the linked list.

          I wonder if I explained that right, but I hope you see the concept.

          --
          Mabden


          Comment

          • Karl Heinz Buchegger

            #6
            Re: how to sort parallel arrays in c++?

            Xiaozhu wrote:[color=blue]
            >
            > say, if you had parallel arrays containing the sales person id, the month, and
            > the sales figures (for that person in that month), sorting on sales figure and
            > preserve the order of figures within the data about the same person:
            >
            > Original Data:
            > id month sales
            > +-----+ +-----+ +-----+
            > | 432 | | 8 | | 89 |
            > +-----+ +-----+ +-----+
            > | 123 | | 8 | | 116 |
            > +-----+ +-----+ +-----+
            > | 555 | | 8 | | 203 |
            > +-----+ +-----+ +-----+
            > | 432 | | 9 | | 105 |
            > +-----+ +-----+ +-----+
            > | 123 | | 9 | | 143 |
            > +-----+ +-----+ +-----+
            > | 555 | | 9 | | 187 |
            > +-----+ +-----+ +-----+
            >
            > Sorted by sales:
            > id month sales
            > +-----+ +-----+ +-----+
            > | 432 | | 8 | | 89 |
            > +-----+ +-----+ +-----+
            > | 432 | | 9 | | 105 |
            > +-----+ +-----+ +-----+
            > | 123 | | 8 | | 116 |
            > +-----+ +-----+ +-----+
            > | 123 | | 9 | | 143 |
            > +-----+ +-----+ +-----+
            > | 555 | | 9 | | 187 |
            > +-----+ +-----+ +-----+
            > | 555 | | 8 | | 203 |
            > +-----+ +-----+ +-----+
            >
            > is that possible to do this in c++? Thanks![/color]

            Sure.
            It's not that hard, if you write the sorting function
            on your own.
            Every sort algorithm needs to do 2 things:
            compare array elements
            eventually swap array elements

            The first one is easy: Just code in on what array wou
            want the comparison be based.
            The second one is easy to: Instead of just swapping
            the entries of a single array, you swap the elements
            of all the parallel arrays at the same index positions:

            ....
            temp = sales[i];
            sales[i] = sales[j];
            sales[j] = temp;

            temp = month[i];
            month[i] = month[j];
            month[j] = temp;

            temp = id[i];
            id[i] = id[j];
            id[j] = temp;
            ....

            The idea is: Whatever you do to one array, you do to all
            of the parallel arrays. This way they stay always in sync.


            *But* - The bigger question is:
            Why do you have parallel arrays in the first place? You
            should have a struct which models a single entry consisting
            of id, month and sales and then have an array of those.

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Rich Grise

              #7
              Re: how to sort parallel arrays in c++?

              On Tuesday 31 August 2004 09:53 am, Xiaozhu did deign to grace us with the
              following:
              [color=blue]
              > say, if you had parallel arrays containing the sales person id, the month,
              > and the sales figures (for that person in that month), sorting on sales
              > figure and preserve the order of figures within the data about the same
              > person:
              >
              > Original Data:
              > id month sales
              > +-----+ +-----+ +-----+
              > | 432 | | 8 | | 89 |[/color]
              ....[color=blue]
              > is that possible to do this in c++? Thanks![/color]

              I'm only a C++ n00b, but I've copied and pasted my fair share of code ;-),
              and the first thing that jumped out at me was "spreadshee t."

              My comment would put the thread OT for the NG, but isn't there a facility
              to bring in objects from other apps' libraries? Just instantiate some kind
              of spreadsheet class. But that's implementation-dependent, so I'll go
              flog myself now. ;-)

              Cheers!
              Rich

              Comment

              Working...