Jump to content

Yo programers, I have a little question.

 

So I have a few arrays of integers, I need to sort one of them and I need to have the other arrays change the coresponding values. So only the first array is sorted and other arrays have their elements in change order the same way the first array changed it's order of elements.

 

The thing is I need to use integrated sort from some library like qsort() from stdlib.h

 

Here is the part of the code I have (it's C programing language):

/*swap function*/void swap(int* a, int i, int j){	int temp;	temp = a[i];	a[i] = a[j];	a[j] = temp;}/*main()*/DDI = (int*)calloc(n, sizeof(int));DDZ = (int*)calloc(n, sizeof(int));ind = (int*)calloc(n, sizeof(int));opt = (int*)calloc(n, sizeof(int));for(i = 0; i < n-1; i++)	for(j = i+1; j < n; j++)	{		if(DDI[i] > DDI[j])		{			swap(DDI, i, j);			swap(DDZ, i, j);			swap(ind, i, j);			swap(opt, i, j);		}	}

It works but I't would be nice to have it done with an integrated function from some library.

 

Thanks for your help in advance and sorry if I wasn't clear enough about what I need :)

Link to comment
https://linustechtips.com/topic/504052-sorting-multiple-array-in-c/
Share on other sites

Link to post
Share on other sites

Is there any reason you can't just use qsort function you mentioned, or are you just looking for an example of how to use it (another)?

Well, it only sorts the array you give it to sort. I need to sort an array but other ones need to have their elements change order.

 

I have looked around the internet and it can be done but not all that different from what I have done in my code so I just gave up and have sent my project like this.

Link to post
Share on other sites

There are a few different ways of doing it :

 

First, you make a backup of your to-be-sorted array. You sort the array as usual , then, for your other array , you search for the new positions of the elements in the original array and move them accordingly. Of course, this won't work if two elements in the original array have the same value in the "hash" array.

 

Secondly, you can make an array of pairs and sort those pairs.

 

Heck, you can even make a hash table for the values in the arrays. Basically :

std::string keys[] = { "1", "2", "3" } ; std::string vals[] = { "a", "b", "c" } ;  std::map< std::string, std::string > hash ;  for( int i = 0 ; i < 3 ; i++ ) {  hash[ keys[i] ] = vals[i] ; }

Sort your array , then get the corresponding hash value;

(yes, I know, std::map is technically a binary search tree , but it has the same functionality as a hash table. If you really want to use hash tables, use std::unordered_map )

 

Or, probably the nicest way of doing it , you can make an array of structures/classes and make a custom sorting function for those.

Here is a quick structure :

struct t{    int x;    char c;    t(int _x , char _c) : x(_x) , c(_c)    {    }};

Now, we'll use std::vectors since we can easily use std::sort on those. Of course, this is C++.

Example :

 vector<t> v = { t(1,'c') , t(5,'f') , t(3, '-') , t(-4 , 'x') };

Then we need to sort it using a custom function. I've used a lambda function since we have that now ( since C++11 ) :D.

sort(v.begin(), v.end() , [](const t r, const t l) {return r.x < l.x;});

Basically, the third parameter is a custom function of yours which , given two parameters , should say if those are in the right order or not . Of course, we are not comparing the structure itself , that currently doesn't make any sense , we have no '<' operator or anything like that on our structure , but we are comparing the 'x' member of the structure.

If you don't understand the lambda :

bool f(const t r, const t l){return r.x < l.x;}/*etc...etc...*/sort(v.begin(), v.end() , f);

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

 

There are a few different ways of doing it :

 

First, you make a backup of your to-be-sorted array. You sort the array as usual , then, for your other array , you search for the new positions of the elements in the original array and move them accordingly. Of course, this won't work if two elements in the original array have the same value in the "hash" array.

 

Secondly, you can make an array of pairs and sort those pairs.

 

Heck, you can even make a hash table for the values in the arrays. Basically :

std::string keys[] = { "1", "2", "3" } ; std::string vals[] = { "a", "b", "c" } ;  std::map< std::string, std::string > hash ;  for( int i = 0 ; i < 3 ; i++ ) {  hash[ keys[i] ] = vals[i] ; }

Sort your array , then get the corresponding hash value;

(yes, I know, std::map is technically a binary search tree , but it has the same functionality as a hash table. If you really want to use hash tables, use std::unordered_map )

 

Or, probably the nicest way of doing it , you can make an array of structures/classes and make a custom sorting function for those.

Here is a quick structure :

struct t{    int x;    char c;    t(int _x , char _c) : x(_x) , c(_c)    {    }};

Now, we'll use std::vectors since we can easily use std::sort on those. Of course, this is C++.

Example :

 vector<t> v = { t(1,'c') , t(5,'f') , t(3, '-') , t(-4 , 'x') };

Then we need to sort it using a custom function. I've used a lambda function since we have that now ( since C++11 ) :D.

sort(v.begin(), v.end() , [](const t r, const t l) {return r.x < l.x;});

Basically, the third parameter is a custom function of yours which , given two parameters , should say if those are in the right order or not . Of course, we are not comparing the structure itself , that currently doesn't make any sense , we have no '<' operator or anything like that on our structure , but we are comparing the 'x' member of the structure.

If you don't understand the lambda :

bool f(const t r, const t l){return r.x < l.x;}/*etc...etc...*/sort(v.begin(), v.end() , f);

 

Oh wow, thank you so much. I apreciate it.

 

That is a smart solution, mine is a bit primitive. Although I've heard of hash tables I didn't really use them anywhere since the profesor didn't show them to us.

I thought that there is something similar but just built in to a library so my sorting code from above would be in just one or two lines since that is what I'm after, minimal code.

 

But hey, thank you for showing this to me. It's nice to know for future tasks :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×