473,382 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

how to efficiently do sorting and get array of indices?

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
3 7089
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Håvard Olerud Eriksen | last post by:
I've got an array of arrays that I need to sort on preferably one of the indices of the nested array, keeping the order of the outside array. The structure is like this: array( => array(key =>...
6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
6
by: praba kar | last post by:
Dear All, I have doubt regarding sorting. I have a list that list have another list (eg) list = ,,] I want to sort only numeric value having array field. How I need to do for that.
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
5
by: mhk | last post by:
Hi , is there any way to merge three sorted arrays into a sorted file, without using 4th array. i guess 3 way merge sort is the only option but i dont know its algorithem. can anyone tell me...
2
by: fred_stevens | last post by:
Hi all you C boffins: I need to sort a vector of doubles is ascending order. Qsort will return the sorted vector, but I need a vector of the indices of the sorted vector, not the actual sorted...
17
by: Umesh | last post by:
I don't understand it. Help me by writing a program while i try myself to sort an array of numbers. Thanks.
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.