MultiDimSort Function for C++

Hi,

Can someone please supply me with a multidimsort function that will enable me to sort a multidimensional array by any specific column? Obviously, the data in each row has to stay aligned - e.g. you have a list of names and telephone numbers in no particular order in an multidim array and you want to sort by the name and still have the same corresponding telephone number.

Thanks,
Malcolm
malramsayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sys_ProgCommented:
That's not a problem

Write a sorting function for single dimensional array and then while swapping the data elements for one dimension, do the swapping for all the dimensions.

Just pass the multi dimension array to be sort, the dimension on which to sort to the sorting function

Since this seems to be a homework, I would stop here

HTH

Amit
0
Sys_ProgCommented:
To add, u can use try with bubblesort first and may be then move on to some more better sorting algorithm


HTH

Amit
0
malramsayAuthor Commented:

Thanks, Amit, but I'd like an example of one if someone has it - the point of the question was so that I wouldn't have to write my own as I'm on a tight time-frame for this project.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Sys_ProgCommented:
Here is the single dimension array sort using BASIC Bubble sort [Enhanced version also exists]

void bubbleSort(int numbers[], int array_size)
{
  int i, j, temp;

  for (i = (array_size - 1); i >= 0; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (numbers[j-1] > numbers[j])
      {
        temp = numbers[j-1];
        numbers[j-1] = numbers[j];
        numbers[j] = temp;
============>>>>
      }
    }
  }
}


Now as I have marked, at that location, u can have swapping for the other dimension as well

Thus if your array is two dimensionsal and u are sorting on FIRST dimension, it would be something like this

if ( arr[0][j-1] > numbers[0][j] )
     {
           temp = arr [0][j-1];
           numbers [0][j-1] = numbers [0][j];
           numbers [0][j] = temp;

           temp = arr [1][j-1] ;
           numbers [1][j-1] = numbers [1][j];
           numbers [1][j] = temp;

     }



here I assume that both your dimension are int's else u will have to make corresponding changes while swapping [i.e.. temp should be the corresponding type as that of the elemnets in that particular dimension]

HTH

Amit
0
malramsayAuthor Commented:
Ok, I think I'm with you.  When you typed,

if ( arr[0][j-1] > numbers[0][j] )

Did you really mean,

if ( numbers[0][j-1] > numbers[0][j] )

What I want to do is sort an array with any 3 columns by the second column, so is this right?

      void bubbleSort(int numbers[], int array_size)
      {
        int i, j, temp;

        for (i = (array_size - 1); i >= 0; i--)
        {
            for (j = 1; j <= i; j++)
            {
              if ( numbers[1][j-1] > numbers[1][j] )
              {
                  temp = numbers[0][j-1];
                  numbers [0][j-1] = numbers [0][j];
                  numbers [0][j] = temp;

                  temp =  numbers[1][j-1] ;
                  numbers [1][j-1] = numbers [1][j];
                  numbers [1][j] = temp;

                  temp =  numbers[2][j-1];
                  numbers [2][j-1] = numbers [2][j];
                  numbers [2][j] = temp;
              }
            }
        }
      }

Now, please don't laugh at my next two rather simple questions, I'm trying to learn C++ by myself and it's rather confusing. I've upped the points a bit cos of these extra 2 questions, but they're very simple!

(1) Just to make sure I get it right, can you give me an example of a call to this function?

(2) What's the difference between passing an array to a function as "int numbers[]" and "int * numbers" as I've seen both?  What, in fact, does * mean? And how does this differ from **?

You'd be surprised how hard it is to find answers one the web to the most simple questions when you're learning something from scratch without any help.

Thanks,
Malcolm



Cheers,
Malcolm
0
Sys_ProgCommented:
Hi Malcolm,

Yes arr means numbers

A couple of observations


1. Your bubblesort function take arrray as a a single dimensional array. As per your requirement it should be a three dimensional array

2.  I am asuuming that You want to sort  the array based on the second dimension
To view it in practiacl sense, it would like You have

Name stored in 1st dimension
Marks stored in 2nd dimension
Grade stored in 3rd dimension

You have such data for around 10 students.

So, based on the marks, you want to have an list in descending order of marks, thus the student and grade should also get swapped

Here's the prototype for this

typedef struct data {
    int col1 ;
    int col2 ;
    itn col3 ;
} data ;

void sort ( data d1[], int len ) {
     int i, j, temp;
     for (i = (len - 1); i >= 0; i--)   {
          for (j = 1; j <= i; j++)    {
              if (d1[j-1].col2 > d1[j].col2 )       {
                     // swap col2
                     temp             =  d1[j-1].col2 ;
                      d1[j-1].col2   =  d1[j].col2  ;
                      d1[j].col2      =  temp;

                     // swap col1
                      temp             =  d1[j-1].col1 ;
                      d1[j-1].col1   =  d1[j].col1  ;
                      d1[j].col1      =  temp;

                     // swap col3
                      temp             =  d1[j-1].col3 ;
                      d1[j-1].col3   =  d1[j].col3  ;
                      d1[j].col3      =  temp;

              }
          }
    }
}


void main ( )  {
      data my_data [10] ;

     my_data[0].col1 = 1 ;
     my_data[0].col2 = 3 ;
     my_data[0].col3 = 4 ;
   
     my_data[1].col1 = 31 ;
     my_data[1].col2 = 34 ;
     my_data[1].col3 = 42 ;

......

     sort ( my_data, 10 ) ;
}


>>(2) What's the difference between passing an array to a function as "int numbers[]" and "int * numbers" as I've seen both?  What, in fact, does * mean? And how does this differ from **?

In C/C++, an array when passed to a function can be treated as an pointer
Thus int *p , int p[] are same when u recieve them in a function parameter list

The only difference is
in case of int *p, u can it in 2 ways

1. For passing an array
2. For passing addr of a single integer

 


HTH

Amit






0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
malramsayAuthor Commented:

Thanks Amit, I'll no doubt have some more questions over the next couple of days as I try to comes to terms with this project.
0
Sys_ProgCommented:
Some links for basic C programming

http://www.howstuffworks.com/c.htm


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.