HP85 GPIB Disk Emulator  1.0
HP85GPIBDiskEmulator
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sort.c
Go to the documentation of this file.
1 
24 #include "user_config.h"
25 
26 #include "sort.h"
27 
29 void heapify(int *v, int size, int root)
30 {
31  int largest = root;
32  int left = (root<<1) + 1;
33  int right = (root<<1) + 2;
34  int tmp;
35 
36 // If left child is larger than root
37  if (left < size && v[left] > v[largest])
38  largest = left;
39 
40 // If right child is larger than largest so far
41  if (right < size && v[right] > v[largest])
42  largest = right;
43 
44 // If largest is not root
45  if (largest != root)
46  {
47 // swap
48  tmp = v[root];
49  v[root] = v[largest];
50  v[largest] = tmp;
51  heapify(v, size, largest);
52  }
53 }
54 
55 
57 void heapsort(int *v, int size)
58 {
59  int root;
60  int tmp;
61 
62  for(root = size / 2 - 1; root >= 0; --root)
63  {
64  heapify(v, size, root);
65  }
66 
67 // One by one extract an element from heap
68  for (root=size-1; root>=0; --root)
69  {
70 // swap
71  tmp = v[0];
72  v[0] = v[root];
73  v[root] = tmp;
74  heapify(v, root, 0);
75  }
76 }
77 
78 
80 void insert_sort(uint16_t *v, int size)
81 {
82  int i,j;
83  uint16_t tmp;
84 
85  for (i = 1; i < size; ++i)
86  {
87  tmp = v[i];
88  for (j = i; j >= 1 && tmp < v[j - 1]; --j)
89  v[j] = v[j - 1];
90  v[j] = tmp;
91  }
92 }
MEMSPACE
#define MEMSPACE
Definition: user_config.h:17
insert_sort
MEMSPACE void insert_sort(uint16_t *v, int size)
Definition: sort.c:80
sort.h
Integer sort.
heapsort
MEMSPACE void heapsort(int *v, int size)
Definition: sort.c:57
heapify
MEMSPACE void heapify(int *v, int size, int root)
Definition: sort.c:29