Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!purdue!mentor.cc.purdue.edu!aic From: aic@mentor.cc.purdue.edu (George A. Basar) Newsgroups: comp.lang.c Subject: Re: Help with qsort Message-ID: <5314@mentor.cc.purdue.edu> Date: 20 Nov 89 22:09:04 GMT References: <1989Nov20.213702.20821@agate.berkeley.edu> Distribution: usa Organization: Buckaroo Banzai Institute Lines: 27 In article <1989Nov20.213702.20821@agate.berkeley.edu>, ilan343@violet.berkeley.edu writes: > I am trying to use the standard C qsort routine to rank an array of > floats. I don't want to disturb the original data, so I tried the > following: > > a) Copy the addresses of each array element into an array of pointers. > b) Invoke qsort on the array of pointers. > c) Print ranks by subtracting pointers No need for step c, the entries in rank are pointers which had been sorted based upon the values in data. > printf ("%d %f %d \n", i, data[i], rank[i] - data); This should be printf ("%d %f %f \n", i, data[i], *rank[i]); Since your compare checks the value in data, qsort swapped pointers in rank. Printing out the values referenced by the elements in rank in order, as above, gives a sorted list. * George A. Basar (317)742-8799 (home) * aic@mentor.cc.purdue.edu BASAR@PURCCVM.BITNET | General Consultant (317)494-1787 (work) | Purdue University Computing Center Disclaimer: My opinions are just that, mine.