/*
  File name: heap.h
  Created by: Ljubomir Buturovic
  Created: 09/25/2002
  Purpose: declarations of functions in heap.c.
*/

/*
  Copyright 2004 Ljubomir J. Buturovic

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

/*
  In-place sort for floating-point 'vector' of length 'len', in
  ascending order.

  The function returns locations of the original numbers after sorting
  if 'index' is not NULL.  For example, if smallest element in
  'vector' is originally in location 5, after sorting index[5] will be
  0. If next-smallest is originally in location 2, index[2] will be 1,
  etc.

  To produce an alternative index, indicating which element goes into
  a given location after sorting, use the following code segment:

	  for (j = 0; j < len; j++)
	    {
	      idx = index[j];
	      isx[idx] = j;
	    }


  Return -1 if 'index' is not NULL, and malloc() fails. Otherwise
  return 0.

  The heapsort algorithm works by first arranging the array into a
  'heap' order. Example heap order for an array of 8 elements a[0..7]:

     a[0] >= a[1]
     a[0] >= a[2]
     a[1] >= a[3]
     a[1] >= a[4]
     a[2] >= a[5]
     a[2] >= a[6]
     a[3] >= a[7]

  Once in heap order, the array is sorted using similar algorithm
  which produced the heap order.

  The algorithm implementation largely follows Algorithm H (Heapsort),
  in: Donald E. Knuth, The Art of Computer Programming, Volume 3,
  Second Edition, Section 5.2.3, p. 145.  Section 8.3,
  Heapsort. Addison-Wesley, 1998.

  Performance comparison for fheap(), sedgsesort() and qsort(), on a
  500 MHz Pentium laptop (times in seconds):

  length     fheap()   sedgesort()    qsort()

  1,000,000     6           2             5
  10,000,000   79          24            37

  In other words, use this function only if you need the index values.  
*/
int fheap(float *vector, int len, int **index);

/*
  In-place sort for double 'vector' of length 'len', in
  ascending order.
*/
int dheap(double *vector, int len, int **index);



syntax highlighted by Code2HTML, v. 0.9.1