|
-
Dec 25th, 2002, 01:17 PM
#1
Thread Starter
Fanatic Member
qsort (quick sort) function
I want to use the built-in qsort function (stdlib.h) to sort a two dimensional array say arr[10][2];
could someone help me write the compare function if arr[m][] goes before arr[n][] if arr[m][0]<arr[n][0]
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Dec 25th, 2002, 03:20 PM
#2
Two-dim arrays are tricky there...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 25th, 2002, 03:28 PM
#3
Frenzied Member
Code:
// call qsort assuming arr[] is integer
qsort(&arr,10,sizeof(arr[0][0])*2, cmp)
int cmp(void *a, void *b){
return ( (int*)a - (int*)b);
}
-
Dec 25th, 2002, 04:01 PM
#4
Expected something along those lines, but I was too tired to think properly about it.
But since he wants to compare the first element of the subarrays this
return ( (int*)a - (int*)b);
should be
return ( (*(int*)a) - (*(int*)b));
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|