Help with qsort

George A. Basar aic at mentor.cc.purdue.edu
Tue Nov 21 09:09:04 AEST 1989


In article <1989Nov20.213702.20821 at agate.berkeley.edu>, ilan343 at 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 at mentor.cc.purdue.edu                     BASAR at PURCCVM.BITNET    
| General Consultant                   	       (317)494-1787 (work)
| Purdue University Computing Center
Disclaimer: My opinions are just that, mine.



More information about the Comp.lang.c mailing list