By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,723 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

how to efficiently do sorting and get array of indices?

P: n/a
In matlab, the sort function returns two things:

[a,b]=sort([5, 8, 7])

a = 5 7 8
b = 1 3 2

where a is the sorted result, and b is the corresponding index.
Is there C or C++ code available to achieve this?
Thanks
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
b8*******@yahoo.com (b83503104) writes:
In matlab, the sort function returns two things:

[a,b]=sort([5, 8, 7])

a = 5 7 8
b = 1 3 2

where a is the sorted result, and b is the corresponding index.
Is there C or C++ code available to achieve this?


The `qsort' function can be used for sorting. If you put each value and
the corresponding index together in a structure object, you can sort
them simultaneously. Write the comparison function so that it only
compares the values, but ignores the indices. See code below for an
example.

(If you're interested in the C++ answer, which is completely different,
ask in comp.lang.c++.)

Martin
#include <stdlib.h>
#include <stdio.h>

struct pair {
int a;
int b;
};

int compare (const void *const first, const void *const second)
{
if (((const struct pair *)first)->a > ((const struct pair *)second)->a)
return 1;
else if (((const struct pair *)first)->a < ((const struct pair *)second)->a)
return -1;
else
return 0;
}

int main (void)
{
const int array [] = {5, 8, 7};
struct pair ab [sizeof array / sizeof *array];
size_t i;

for (i = 0; i < sizeof array / sizeof *array; ++i)
{
ab [i].a = array [i];
ab [i].b = i + 1;
}

qsort (ab, sizeof ab / sizeof *ab, sizeof *ab, compare);

fputs ("a =", stdout);
for (i = 0; i < sizeof ab / sizeof *ab; ++i)
printf (" %d", ab [i].a);
fputs ("\nb =", stdout);
for (i = 0; i < sizeof ab / sizeof *ab; ++i)
printf (" %d", ab [i].b);
putchar ('\n');

return 0;
}
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #2

P: n/a
b8*******@yahoo.com (b83503104) writes:
In matlab, the sort function returns two things:

[a,b]=sort([5, 8, 7])

a = 5 7 8
b = 1 3 2

where a is the sorted result, and b is the corresponding index.
Is there C or C++ code available to achieve this?


Sort an array of indexes using a comparison function that
compares the indexed items. Afterward, the array contains the
sorted indexes and you can easily find their corresponding items.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #3

P: n/a


b83503104 wrote:
In matlab, the sort function returns two things:

[a,b]=sort([5, 8, 7])

a = 5 7 8
b = 1 3 2

where a is the sorted result, and b is the corresponding index.
Is there C or C++ code available to achieve this?
Thanks


Declare an array of pointers that point to the address of each
element of the int array. Using qsort, sort the array of pointers
and use pointer math to calculate the corresponding index.

#include <stdio.h>
#include <stdlib.h>

#define ARRSZ 16

int cmp(const void *v1, const void *v2)
{
const int i1 = **(const int **)v1;
const int i2 = **(const int **)v2;

return i1<i2?-1:(i1>i2);
}

int main(void)
{
int array[ARRSZ] = {5,7,3,8,2,5,9,1,55,25,66,44,42,4,10,11};
int *parray[ARRSZ],i;

for(i = 0; i < ARRSZ;i++) parray[i] = &array[i];
qsort(parray,ARRSZ, sizeof *parray,cmp);
puts("Sorting the values:");
for(i = 0;i < ARRSZ;i++) printf("%d ",array[i]);
puts("\n");
for(i = 0;i < ARRSZ;i++)
printf("value: %-6d position: %d\n",*parray[i],
(parray[i]-array)+1);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.