VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
heapsort.c File Reference
#include "heapsort.h"
+ Include dependency graph for heapsort.c:

Go to the source code of this file.

Functions

static void add_to_sort_heap (int *heap, float *sort_values, int index, int heap_tail)
 
static int get_top_of_heap_index (int *heap, float *sort_values, int heap_tail, int start_index)
 
void heapsort (int *sort_index, float *sort_values, int nelem, int start_index)
 

Function Documentation

static void add_to_sort_heap ( int *  heap,
float *  sort_values,
int  index,
int  heap_tail 
)
static

Definition at line 42 of file heapsort.c.

43  {
44 
45  /* Adds element index, with value = sort_values[index] to the heap. *
46  * Heap_tail is the number of elements in the heap *after* the insert. */
47 
48  unsigned int ifrom, ito; /* Making these unsigned helps compiler opts. */
49  int temp;
50 
51  heap[heap_tail] = index;
52  ifrom = heap_tail;
53  ito = ifrom / 2;
54 
55  while (ito >= 1 && sort_values[heap[ifrom]] < sort_values[heap[ito]]) {
56  temp = heap[ito];
57  heap[ito] = heap[ifrom];
58  heap[ifrom] = temp;
59  ifrom = ito;
60  ito = ifrom / 2;
61  }
62 }
static struct s_heap ** heap
Definition: route_common.c:29
static int heap_tail
Definition: route_common.c:31

+ Here is the caller graph for this function:

static int get_top_of_heap_index ( int *  heap,
float *  sort_values,
int  heap_tail,
int  start_index 
)
static

Definition at line 64 of file heapsort.c.

65  {
66 
67  /* Returns the index of the item at the top of the heap (the smallest one). *
68  * It then rebuilds the heap. Heap_tail is the number of items on the heap *
69  * before the top item is deleted. */
70 
71  int index_of_smallest, temp;
72  unsigned int ifrom, ito, heap_end;
73 
74  index_of_smallest = heap[1];
75  heap[1] = heap[heap_tail];
76  heap_end = heap_tail - 1; /* One item deleted. */
77  ifrom = 1;
78  ito = 2 * ifrom;
79 
80  while (ito <= heap_end) {
81  if (sort_values[heap[ito + 1]] < sort_values[heap[ito]])
82  ito++;
83 
84  if (sort_values[heap[ito]] > sort_values[heap[ifrom]])
85  break;
86 
87  temp = heap[ito];
88  heap[ito] = heap[ifrom];
89  heap[ifrom] = temp;
90  ifrom = ito;
91  ito = 2 * ifrom;
92  }
93 
94  /*index must have the start_index subracted to be consistent with the original array*/
95  return (index_of_smallest - start_index);
96 }
static struct s_heap ** heap
Definition: route_common.c:29
static int heap_tail
Definition: route_common.c:31

+ Here is the caller graph for this function:

void heapsort ( int *  sort_index,
float *  sort_values,
int  nelem,
int  start_index 
)

Definition at line 13 of file heapsort.c.

13  {
14 
15  /* This routine loads sort_index [1..nelem] with nelem values: the indices *
16  * of the sort_values [1..nelem] array that lead to sort_values[index] being *
17  * decreasing as one goes through sort_index. Sort_values is not changed. */
18 
19  int i;
20 
21  sort_index -= start_index;
22  sort_values -= start_index;
23 
24  /* Build a heap with the *smallest* element at the top. */
25 
26  for (i = 1; i <= nelem; i++)
27  add_to_sort_heap(sort_index, sort_values, i, i);
28 
29  /* Now pull items off the heap, smallest first. As the heap (in sort_index) *
30  * shrinks, I store the item pulled off (the smallest) in sort_index, *
31  * starting from the end. The heap and the stored smallest values never *
32  * overlap, so I get a nice sort-in-place. */
33 
34  for (i = nelem; i >= 1; i--)
35  sort_index[i] = get_top_of_heap_index(sort_index, sort_values, i,
36  start_index);
37 
38  sort_index += start_index;
39  sort_values += start_index;
40 }
static void add_to_sort_heap(int *heap, float *sort_values, int index, int heap_tail)
Definition: heapsort.c:42
static int get_top_of_heap_index(int *heap, float *sort_values, int heap_tail, int start_index)
Definition: heapsort.c:64

+ Here is the call graph for this function:

+ Here is the caller graph for this function: