diff options
Diffstat (limited to 'src/03/sort.c')
| -rw-r--r-- | src/03/sort.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/03/sort.c b/src/03/sort.c index 61b12de..d1ae704 100644 --- a/src/03/sort.c +++ b/src/03/sort.c @@ -1,6 +1,13 @@ #include <stdio.h> #include <stdlib.h> +/** + * Prints a visual dump of an array of + * items to stdout out for debugging. + * + * @param items The items to inspect + * @param n The total # of items in the array. + */ static void dump(int *items, int n) { printf("["); for (int i = 0; i < n; ++i) @@ -8,6 +15,14 @@ static void dump(int *items, int n) { printf("]\n"); } +/** + * Merges two subsequences of an array. + * + * @params items A pointer to the start of an array of items + * @param min The starting index of the left sub sequence. + * @param mid The starting index of the right sub sequence. + * @param max The ending index of the right sub sequence. + */ static void _merge(int *items, int min, int mid, int max) { int length = (max - min) + 1; int tmp[length]; @@ -29,6 +44,13 @@ static void _merge(int *items, int min, int mid, int max) { items[min + i] = tmp[i]; } +/** + * Performs a recursive merge sort of items in an array. + * + * @param items An array of integers. + * @param min The starting index of a subsequence to sort + * @param max The ending index of a subsequence to sort + */ static void _merge_sort(int *items, int min, int max) { if (min >= max) return; @@ -39,6 +61,15 @@ static void _merge_sort(int *items, int min, int max) { _merge(items, min, mid + 1, max); } +/** + * Partitions a sequence into two subsequences. + * + * @param items An array of integers to partition + * @param min The starting index of the sequence to partition + * @param max The ending index of the sequence to partition + * @return Returns the index that can be used as the partition point for the + * sequence. + */ static int partition(int *items, int min, int max) { int pivot = items[max]; int index = min - 1; @@ -59,6 +90,13 @@ static int partition(int *items, int min, int max) { return index + 1; } +/** + * Performs a recursive quick sort on an array of items. + * + * @param items An array of integers + * @param min The starting index of a subsequence to sort + * @param max The ending index of a subsequence to sort + */ static void _quick_sort(int *items, int min, int max) { if (min >= max) return; @@ -68,6 +106,12 @@ static void _quick_sort(int *items, int min, int max) { _quick_sort(items, index + 1, max); } +/** + * Performs a merge sort on an array of integers. + * + * @param items An array of integers + * @param length The # of items in the array of integers + */ void merge_sort(int *items, int length) { if (!items || length <= 0) return; @@ -75,6 +119,12 @@ void merge_sort(int *items, int length) { _merge_sort(items, 0, length - 1); } +/** + * Performs a quick sort on an array of integers. + * + * @param items An array of integers + * @param length The # of items in the array of integers + */ void quick_sort(int *items, int length) { if (!items || length <= 0) return; |
