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
Solved

MultiDimSort Function for C++

Posted on 2003-12-09
8
288 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

809 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