home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!purdue!mentor.cc.purdue.edu!noose.ecn.purdue.edu!ecn.purdue.edu!mccauley
- From: mccauley@ecn.purdue.edu (Darrell McCauley)
- Subject: Re: qsort/pointer question (should be in FAQ?)
- Message-ID: <1993Jan21.102641.14862@noose.ecn.purdue.edu>
- Followup-To: comp.lang.c
- Sender: news@noose.ecn.purdue.edu (USENET news)
- Organization: Ag Engineering Dept, Purdue University
- References: <1993Jan20.213331.5197@noose.ecn.purdue.edu> <C16Boy.Lo5@portal.hq.videocart.com>
- Date: Thu, 21 Jan 1993 10:26:41 GMT
- Lines: 56
-
- In article <C16Boy.Lo5@portal.hq.videocart.com>, dfuller@portal.hq.videocart.com (Dave Fuller) writes:
- |> mccauley@ecn.purdue.edu (Darrell McCauley) writes:
- |> : sorry for what is probably a simple question, but qsort always
- |> : gives me trouble. I need to sort a 2 dimensional double array:
- |> :
- |> : qsort (z, 10, 2*sizeof (double), zcmp);
- |> :
- |> : and I cannot figure out how to write the comparison function.
- |> : My attempt at sorting by the first column is below. Can you
- |> : fix it? What if I wanted to sort by the second column?
-
- |> say we have a 2-d char array (each char being 1-byte for example sake)
- |>
- |> char x[2][5];
- |>
- |> what it looks like in memory is: 11111 (1st row, followed by . . )
- |> 22222 (2nd row)
- |> these will be contiguous. (1111122222) in memory.
-
- Is this the case when the array is dynamically allocated? According to the
- FAQ, you have to allocate nrows*ncolumns in one malloc call to get
- contiguous memory chunks (even then, though, are you assured of getting
- contiguous memory? If nrows*ncolumns is too large of a chunk, won't
- unix split it up among different memory locations?). Anyway, sticking the
- the problem at hand, I'm doing memory allocation the normal way:
-
- double **array;
- array = (double **)malloc(nrows * sizeof(double *));
- for(i = 0; i < nrows; ++i)
- array[i] = (double *)calloc(ncolumns * sizeof(double));
-
- If memory is allocated a another way (or if pointers are
- rearranged), couldn't qsort be usable? qsort is attractive,
- not because of performance, but availability.
-
- |> By telling qsort (2 * sizeof(char)) for the element size it will step
- |> through the data as follows
- |>
- |> 1111122222
- |> ^ ^ ^ ^ ^
- |>
- |> definitely NOT what you want.
-
- |> If you want to do a 2-d array sort (by row i assume), you are going to
- |> be writing your own routines to do it.
-
- This doesn't sound too enjoyable. If qsort just absolutely cannot be used,
- does anyone have sources for other sorts that are completely freely
- distributable?
-
- |> Dave Fuller
- |> dfuller@portal.hq.videocart.com
- --
- James Darrell McCauley Dept of Ag Engineering, Purdue Univ
- internet: mccauley@ecn.purdue.edu West Lafayette, Indiana 47907-1146, USA
- bitnet: mccauley%ecn@purccvm UUCP: pur-ee!mccauley
-