home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20011 < prev    next >
Encoding:
Text File  |  1993-01-21  |  2.8 KB  |  69 lines

  1. Newsgroups: comp.lang.c
  2. 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
  3. From: mccauley@ecn.purdue.edu (Darrell McCauley)
  4. Subject: Re: qsort/pointer question (should be in FAQ?)
  5. Message-ID: <1993Jan21.102641.14862@noose.ecn.purdue.edu>
  6. Followup-To: comp.lang.c
  7. Sender: news@noose.ecn.purdue.edu (USENET news)
  8. Organization: Ag Engineering Dept, Purdue University 
  9. References: <1993Jan20.213331.5197@noose.ecn.purdue.edu> <C16Boy.Lo5@portal.hq.videocart.com>
  10. Date: Thu, 21 Jan 1993 10:26:41 GMT
  11. Lines: 56
  12.  
  13. In article <C16Boy.Lo5@portal.hq.videocart.com>, dfuller@portal.hq.videocart.com (Dave Fuller) writes:
  14. |> mccauley@ecn.purdue.edu (Darrell McCauley) writes:
  15. |> : sorry for what is probably a simple question, but qsort always
  16. |> : gives me trouble. I need to sort a 2 dimensional double array:
  17. |> : 
  18. |> :   qsort (z, 10, 2*sizeof (double), zcmp);
  19. |> : 
  20. |> : and I cannot figure out how to write the comparison function.
  21. |> : My attempt at sorting by the first column is below. Can you
  22. |> : fix it?  What if I wanted to sort by the second column?
  23.  
  24. |> say we have a 2-d char array (each char being 1-byte for example sake)
  25. |> 
  26. |> char x[2][5];
  27. |> 
  28. |> what it looks like in memory is: 11111  (1st row, followed by . . )
  29. |>                                  22222  (2nd row)
  30. |> these will be contiguous. (1111122222) in memory.
  31.  
  32. Is this the case when the array is dynamically allocated? According to the
  33. FAQ, you have to allocate nrows*ncolumns in one malloc call to get
  34. contiguous memory chunks (even then, though, are you assured of getting
  35. contiguous memory? If nrows*ncolumns is too large of a chunk, won't
  36. unix split it up among different memory locations?). Anyway, sticking the
  37. the problem at hand, I'm doing memory allocation the normal way:
  38.  
  39.          double **array;
  40.      array = (double **)malloc(nrows * sizeof(double *));
  41.          for(i = 0; i < nrows; ++i)
  42.            array[i] = (double *)calloc(ncolumns * sizeof(double));
  43.  
  44. If memory is allocated a another way (or if pointers are
  45. rearranged), couldn't qsort be usable?  qsort is attractive, 
  46. not because of performance, but availability.
  47.  
  48. |> By telling qsort (2 * sizeof(char)) for the element size it will step 
  49. |> through the data as follows
  50. |> 
  51. |> 1111122222
  52. |> ^ ^ ^ ^ ^
  53. |> 
  54. |> definitely NOT what you want.
  55.  
  56. |> If you want to do a 2-d array sort (by row i assume), you are going to
  57. |> be writing your own routines to do it. 
  58.  
  59. This doesn't sound too enjoyable. If qsort just absolutely cannot be used,
  60. does anyone have sources for other sorts that are completely freely 
  61. distributable?
  62.  
  63. |> Dave Fuller
  64. |> dfuller@portal.hq.videocart.com
  65. -- 
  66. James Darrell McCauley                Dept of Ag Engineering, Purdue Univ
  67. internet: mccauley@ecn.purdue.edu     West Lafayette, Indiana 47907-1146, USA
  68. bitnet: mccauley%ecn@purccvm          UUCP: pur-ee!mccauley
  69.