1 #ifndef ASSORTED_SORTS_GROUP
2 #define ASSORTED_SORTS_GROUP
21 #include <system_helper.h>
41 for (
int i = 0; i < size; i++) {
63 for (gap = n / 2; gap > 0; gap /= 2) {
67 for (i = gap; i < n; i++) {
72 for (j = i - gap; j >= 0 && v[j] > v[j + gap]; j = j - gap) {
80 for (j = i - gap; j >= 0 && v[j] < v[j + gap]; j = j - gap) {
104 if (first.
length() <= 0) {
106 to_return += second[0];
108 }
else if (second.
length() <= 0) {
109 to_return += first[0];
111 }
else if ((!reverse && (first[0] <= second[0])) || (reverse && (first[0] >= second[0]))) {
113 to_return += first[0];
117 to_return += second[0];
136 int midway = v.
length() / 2;
139 return merge(firstPart, secondPart, reverse);
159 heap(type to_sort[],
int n,
bool reverse)
163 _heapspace = to_sort;
170 type temp = _heapspace[a];
171 _heapspace[a] = _heapspace[b];
172 _heapspace[b] = temp;
216 if ((!_reverse && (_heapspace[swap] > _heapspace[child]))
217 || (_reverse && (_heapspace[swap] < _heapspace[child])))
222 if ((child + 1 <= end)
223 && ((!_reverse && (_heapspace[swap] > _heapspace[child + 1]))
224 || (_reverse && (_heapspace[swap] < _heapspace[child + 1]))))
244 while (end < _total) {
255 while (child > start) {
257 if ((!_reverse && (_heapspace[parent] < _heapspace[child]))
258 || (_reverse && (_heapspace[parent] > _heapspace[child])))
305 type temp = array[a];
312 int partition(type a[],
int start,
int end,
bool reverse)
315 int pivot = a[start];
321 }
while ((!reverse && (a[i] < pivot)) || (reverse && (a[i] > pivot)));
324 }
while ((!reverse && (a[j] > pivot)) || (reverse && (a[j] < pivot)));
340 int pivot =
partition(v, start, end, reverse);
352 template <
class type>
361 template <
class type>
365 for (
int i = 0; i < n; i++) {
368 int swap_index =
randomizer.inclusive(i, n - 1);
void alt_heapify()
re-sorts the heapspace to maintain the heap ordering. this uses sift_up.
int right_child(int i)
returns the right child of node at position i.
heap(type to_sort[], int n, bool reverse)
void sift_down(int start, int end)
void heapify()
re-sorts the heapspace to maintain the heap ordering.
void sift_up(int start, int end)
start is how far up in the heap to sort. end is the node to sift.
void swap_values(int a, int b)
swaps the values in the heap stored at positions a and b.
int left_child(int i)
returns the left child of node at position i.
int parent_index(int i)
get the index of the parent of the node at i.
a_sprintf is a specialization of astring that provides printf style support.
Represents a sequential, ordered, contiguous collection of objects.
array subarray(int start, int end) const
Returns the array segment between the indices "start" and "end".
int length() const
Returns the current reported length of the allocated C array.
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
Provides a dynamically resizable ASCII character string.
a platform-independent way to acquire random numbers in a specific range.
void swap_values(type array[], int a, int b)
swaps the values in the array stored at positions a and b.
void randomize_list(type v[], int n)
handy method for randomizing the order of a list. not strictly a sorting function....
void heap_sort(type v[], int n, bool reverse=false)
int partition(type a[], int start, int end, bool reverse)
void quick_sort(type v[], int n, bool reverse=false)
void inner_quick_sort(type v[], int start, int end, bool reverse)
the recursive version of quick sort that does the work for our convenience method.
void shell_sort(type v[], int n, bool reverse=false)
shell sort algorithm.
basis::array< type > merge_sort(const basis::array< type > &v, bool reverse=false)
basis::array< type > merge(basis::array< type > &first, basis::array< type > &second, bool reverse)
basis::astring dump_list(type v[], int size)
dumps the contents of the list out, assuming that the type can be turned into an int.