home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!usc!rpi!batcomputer!munnari.oz.au!goanna!ok
- From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
- Newsgroups: comp.lang.c
- Subject: Re: How can one make a dynamic array?
- Message-ID: <14366@goanna.cs.rmit.oz.au>
- Date: 7 Sep 92 05:59:06 GMT
- References: <aphaswan.1.0@cs1.uct.ac.za>
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Lines: 50
-
- In article <aphaswan.1.0@cs1.uct.ac.za>, aphaswan@cs1.uct.ac.za (AK PHASWANA) writes:
- > Most sorting procedures like bubble sort, bisection method, merge sort,
- > use arrays.
-
- Only the versions that are set up to sort arrays.
- Bubble sort (does anyone ever _use_ it other than as a horrible example of
- what _not_ to do), insertion sort, selection sort, heap sort, quick sort,
- merge sort, all work perfectly well with linked lists. About the only
- well-known sorting method that even begins to be tricky with lists is
- Shell sort. I haven't heard of any sorting algorithm called "bisection
- method", how does it work?
-
- > The problem with arrays is that they are not dynamic.
-
- Only in antique languages like C and Pascal. Earlier languages like
- Algol 60, PL/I, and Algol 68 let you declare arrays whose size is not
- known until run-time. Newer languages like Ada, Fortran, and GNU C (the
- language implemented by GCC) also allow this. For example, in GNU C you
- can write
-
- void sort(int n, EltType a[]) {
- auto EltType work[n];
- ...
- }
-
- Even in C you can use malloc() to make an array of whatever size you want
- (apart from addressing problems on i8086-based machines):
- void sort(int n, EltType a[]) {
- auto EltType *work = malloc(n * sizeof *work);
- assert(work != NULL);
- ... use work[] ...
- free(work);
- }
- Having to release the storage yourself is a pain and a common source
- of errors. Viva GNU C! (Come to that, viva Ada!)
-
- > Or is there any way of implementic sorting and searching procedures
- > without using arrays?
-
- For many applications you will find one-way linked lists a _superb_ data
- structure. Merge sort in particular (one of the most efficient and
- adaptable sorting methods around, demonstrably faster than quicksort)
- just loves linked lists.
-
- For searching, you might as well use red-black trees or splay trees or
- even AVL trees; as dynamic as you please and O(lgN) lookup and update.
- See any good data structures textbook, e.g. Sedgewick's "Algorithms in C".
-
- --
- You can lie with statistics ... but not to a statistician.
-