00001 /************************************************************************************************** 00002 Software License Agreement (BSD License) 00003 00004 Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt 00005 All rights reserved. 00006 00007 Redistribution and use in source and binary forms, with or without modification, are permitted 00008 provided that the following conditions are met: 00009 00010 *Redistributions of source code must retain the above copyright notice, this list of 00011 conditions and the following disclaimer. 00012 *Redistributions in binary form must reproduce the above copyright notice, this list of 00013 conditions and the following disclaimer in the documentation and/or other materials provided 00014 with the distribution. 00015 *Neither the name of the University of Aveiro nor the names of its contributors may be used to 00016 endorse or promote products derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 00019 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00021 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00024 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00025 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 ***************************************************************************************************/ 00027 /* 00028 * This program is free software; you can redistribute it and/or modify 00029 * it under the terms of the GNU General Public License as published by 00030 * the Free Software Foundation; either version 2 of the License, or 00031 * (at your option) any later version. 00032 * 00033 * This program is distributed in the hope that it will be useful, 00034 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00036 * GNU General Public License for more details. 00037 * 00038 * You should have received a copy of the GNU General Public License 00039 * along with this program; if not, write to the Free Software 00040 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00041 * 00042 */ 00043 00044 #include <stdio.h> 00045 #include "percolate.h" 00046 00052 void swapItem(TAsoc *a, TAsoc *b){ 00053 TAsoc c; 00054 00055 c=*a; 00056 *a=*b; 00057 *b=c; 00058 } 00059 00060 void perc_down(TAsoc a[], int i, int n) { 00061 int child; TAsoc tmp; 00062 for (tmp=a[i]; i*2 <= n; i=child) { 00063 child = i*2; 00064 if ((child != n) && (a[child+1].dist > a[child].dist)) 00065 child++; 00066 if (tmp.dist < a[child].dist) 00067 a[i] = a[child]; 00068 else 00069 break; 00070 } 00071 a[i] = tmp; 00072 } 00073 00074 void heapsort(TAsoc a[], int n) { 00075 int i, j; 00076 j = n; 00077 for (i=n/2; i>0; i--) /* BuildHeap */ 00078 perc_down(a,i,j); 00079 i = 1; 00080 for (j=n; j>=2; j--) { 00081 swapItem(&a[i],&a[j]); /* DeleteMax */ 00082 perc_down(a,i,j-1); 00083 } 00084 }