how to efficiently do sorting and get array of indices?

B

b83503104

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
 
B

Ben Pfaff

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.
 
M

Martin Dickopp

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 .a = array ;
ab .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 .a);
fputs ("\nb =", stdout);
for (i = 0; i < sizeof ab / sizeof *ab; ++i)
printf (" %d", ab .b);
putchar ('\n');

return 0;
}
 
A

Al Bowers

b83503104 said:
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 = &array;
qsort(parray,ARRSZ, sizeof *parray,cmp);
puts("Sorting the values:");
for(i = 0;i < ARRSZ;i++) printf("%d ",array);
puts("\n");
for(i = 0;i < ARRSZ;i++)
printf("value: %-6d position: %d\n",*parray,
(parray-array)+1);
return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top