#include "heapsort.h"
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) |
void | heapsort (int *sort_index, float *sort_values, int nelem) |
static void add_to_sort_heap | ( | int * | heap, | |
float * | sort_values, | |||
int | index, | |||
int | heap_tail | |||
) | [static] |
Definition at line 46 of file heapsort.c.
00050 { 00051 00052 /* Adds element index, with value = sort_values[index] to the heap. * 00053 * Heap_tail is the number of elements in the heap *after* the insert. */ 00054 00055 unsigned int ifrom, ito; /* Making these unsigned helps compiler opts. */ 00056 int temp; 00057 00058 heap[heap_tail] = index; 00059 ifrom = heap_tail; 00060 ito = ifrom / 2; 00061 00062 while(ito >= 1 && sort_values[heap[ifrom]] < sort_values[heap[ito]]) 00063 { 00064 temp = heap[ito]; 00065 heap[ito] = heap[ifrom]; 00066 heap[ifrom] = temp; 00067 ifrom = ito; 00068 ito = ifrom / 2; 00069 } 00070 }
static int get_top_of_heap_index | ( | int * | heap, | |
float * | sort_values, | |||
int | heap_tail | |||
) | [static] |
Definition at line 74 of file heapsort.c.
00077 { 00078 00079 /* Returns the index of the item at the top of the heap (the smallest one). * 00080 * It then rebuilds the heap. Heap_tail is the number of items on the heap * 00081 * before the top item is deleted. */ 00082 00083 int index_of_smallest, temp; 00084 unsigned int ifrom, ito, heap_end; 00085 00086 index_of_smallest = heap[1]; 00087 heap[1] = heap[heap_tail]; 00088 heap_end = heap_tail - 1; /* One item deleted. */ 00089 ifrom = 1; 00090 ito = 2 * ifrom; 00091 00092 while(ito <= heap_end) 00093 { 00094 if(sort_values[heap[ito + 1]] < sort_values[heap[ito]]) 00095 ito++; 00096 00097 if(sort_values[heap[ito]] > sort_values[heap[ifrom]]) 00098 break; 00099 00100 temp = heap[ito]; 00101 heap[ito] = heap[ifrom]; 00102 heap[ifrom] = temp; 00103 ifrom = ito; 00104 ito = 2 * ifrom; 00105 } 00106 00107 return (index_of_smallest); 00108 }
void heapsort | ( | int * | sort_index, | |
float * | sort_values, | |||
int | nelem | |||
) |
Definition at line 19 of file heapsort.c.
00022 { 00023 00024 /* This routine loads sort_index [1..nelem] with nelem values: the indices * 00025 * of the sort_values [1..nelem] array that lead to sort_values[index] being * 00026 * decreasing as one goes through sort_index. Sort_values is not changed. */ 00027 00028 int i; 00029 00030 /* Build a heap with the *smallest* element at the top. */ 00031 00032 for(i = 1; i <= nelem; i++) 00033 add_to_sort_heap(sort_index, sort_values, i, i); 00034 00035 /* Now pull items off the heap, smallest first. As the heap (in sort_index) * 00036 * shrinks, I store the item pulled off (the smallest) in sort_index, * 00037 * starting from the end. The heap and the stored smallest values never * 00038 * overlap, so I get a nice sort-in-place. */ 00039 00040 for(i = nelem; i >= 1; i--) 00041 sort_index[i] = get_top_of_heap_index(sort_index, sort_values, i); 00042 }