Solved

MultiDimSort Function for C++

Posted on 2003-12-09
8
301 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:malramsay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9909626
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9909630
To add, u can use try with bubblesort first and may be then move on to some more better sorting algorithm


HTH

Amit
0
 

Author Comment

by:malramsay
ID: 9909826

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9909885
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
 

Author Comment

by:malramsay
ID: 9909960
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
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 175 total points
ID: 9910052
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
 

Author Comment

by:malramsay
ID: 9910087

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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9910117
Some links for basic C programming

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


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question