Solved

MultiDimSort Function for C++

Posted on 2003-12-09
8
273 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now