percolate.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "percolate.h"
3 
4 
5 void swapItem(TAsoc *a, TAsoc *b){
6  TAsoc c;
7 
8  c=*a;
9  *a=*b;
10  *b=c;
11 }
12 
13 void perc_down(TAsoc a[], int i, int n) {
14  int child; TAsoc tmp;
15  for (tmp=a[i]; i*2 <= n; i=child) {
16  child = i*2;
17  if ((child != n) && (a[child+1].dist > a[child].dist))
18  child++;
19  if (tmp.dist < a[child].dist)
20  a[i] = a[child];
21  else
22  break;
23  }
24  a[i] = tmp;
25 }
26 
27 void heapsort(TAsoc a[], int n) {
28  int i, j;
29  j = n;
30  for (i=n/2; i>0; i--) /* BuildHeap */
31  perc_down(a,i,j);
32  i = 1;
33  for (j=n; j>=2; j--) {
34  swapItem(&a[i],&a[j]); /* DeleteMax */
35  perc_down(a,i,j-1);
36  }
37 }
float dist
Definition: TData.h:57
void swapItem(TAsoc *a, TAsoc *b)
Definition: percolate.c:5
void heapsort(TAsoc a[], int n)
Definition: percolate.c:27
void perc_down(TAsoc a[], int i, int n)
Definition: percolate.c:13
Definition: TData.h:56


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23