Sort 1-D of a 2-D array and copy both

I

IN_C_CRADDLE

Hello all,
I am newbie trying to sort as follows :

Before:
int a[9][2] = {0,5,
0,10,
1,2,
0,1,
0,8,
1,9,
1,12,
0,11,
0,15};

After Sorting:

a[9][2] = {
0,1,
1,2,
0,5,
0,8,
1,9,
0,10,
0,11,
1,12,
0,15};

I want to sort a[][1] in in ascending order and copy both a[][0] and a[][1].
Please help me.

~regards,
Dilip
 
M

Michael Mair

Hiho,

IN_C_CRADDLE said:
Hello all,
I am newbie trying to sort as follows :

Before:
int a[9][2] = {0,5,
0,10,
1,2,
0,1,
0,8,
1,9,
1,12,
0,11,
0,15};

After Sorting:

a[9][2] = {
0,1,
1,2,
0,5,
0,8,
1,9,
0,10,
0,11,
1,12,
0,15};

I want to sort a[][1] in in ascending order and copy both a[][0] and a[][1].

Do you mean you want to sort using "a[][1]" as sort key and the
arrays of two integers to be sorted?

Then use qsort() from <stdlib.h> and give it a suitable compar()
function:
#include <stdlib.h>
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
Read the manpage or the documentation in a C book of your choice.

Here:

base: a
nmemb: 9
size: 2*sizeof(int)
compar: pointer to a function comparing two objects
e.g.
int cmp_comp1 (const void *a, const void *b)
{
int val_a, val_b;

val_a = ((const int *) a)[1];
val_b = ((const int *) b)[1];

if (val_a < val_b)
return -1;
else if (val a > val b)
return 1;
else return 0;
}

I did not test the code, but I think you can get the idea from
what I wrote.


Cheers
Michael
 
H

hari4063

You can do this by writing your own sort, like:

void sort(int a[][2], int len) {
int i, j, tmp;
for(i=0; i<len; i++)
for(j=i+1; j<len; j++)
if (a[1] > a[j][1]) {
tmp=a[1]; a[1]=a[j][1];
a[j][1]=tmp;
}
}

of course, find some better algo (like Quick Sort).
 
F

Flash Gordon

On 27 Oct 2004 05:41:03 -0700
Hello all,
I am newbie trying to sort as follows :

Before:
int a[9][2] = {0,5,

I want to sort a[][1] in in ascending order and copy both a[][0] and
a[][1]. Please help me.

Sounds a lot like homework to me. I'll do it for 100UKP (normally you
get charged in the UK as if the exchange rate with the USD was 1).

Alternatively you could try looking up sorting in K&R (read the FAQ for
comp.lang.c if you don't know what K&R is, and read it anyway even if
you do) or any other text book, then come back here with your attempt at
implementing it once you have made the attempt.
 
B

Barry Schwarz

You can do this by writing your own sort, like:

void sort(int a[][2], int len) {
int i, j, tmp;
for(i=0; i<len; i++)
for(j=i+1; j<len; j++)
if (a[1] > a[j][1]) {
tmp=a[1]; a[1]=a[j][1];
a[j][1]=tmp;


Since he wants to keep each a[n][0]-a[n][1] pair together, you need to
also swap a[0] with a[j][0] just as you did for a[1] and
a[j][1].
}
}

of course, find some better algo (like Quick Sort).



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top