VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
heapsort.c
Go to the documentation of this file.
1 #include "heapsort.h"
2 
3 /******************* Subroutines local to heapsort.c ************************/
4 
5 static void add_to_sort_heap(int *heap, float *sort_values, int index,
6  int heap_tail);
7 
8 static int get_top_of_heap_index(int *heap, float *sort_values, int heap_tail,
9  int start_index);
10 
11 /********************* Subroutine definitions *******************************/
12 
13 void heapsort(int *sort_index, float *sort_values, int nelem, int start_index) {
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 }
41 
42 static void add_to_sort_heap(int *heap, float *sort_values, int index,
43  int heap_tail) {
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 }
63 
64 static int get_top_of_heap_index(int *heap, float *sort_values, int heap_tail,
65  int start_index) {
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 }
void heapsort(int *sort_index, float *sort_values, int nelem, int start_index)
Definition: heapsort.c:13
int index
Definition: hash.h:5
static void add_to_sort_heap(int *heap, float *sort_values, int index, int heap_tail)
Definition: heapsort.c:42
static struct s_heap ** heap
Definition: route_common.c:29
static int get_top_of_heap_index(int *heap, float *sort_values, int heap_tail, int start_index)
Definition: heapsort.c:64
static int heap_tail
Definition: route_common.c:31