diff --git a/Makefile b/Makefile index 93c2361..3a82799 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,12 @@ INCLUDE_INSTALL_PATH := /usr/local/include/lsort LIB_INSTALL_PATH := /usr/local/lib STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \ - $(STATIC_BUILD_DIR)/types.o + $(STATIC_BUILD_DIR)/types.o \ + $(STATIC_BUILD_DIR)/radix.o DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \ - $(STATIC_BUILD_DIR)/types.o + $(STATIC_BUILD_DIR)/types.o \ + $(STATIC_BUILD_DIR)/radix.o TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \ diff --git a/include/lsort.h b/include/lsort.h index cc625a2..1755533 100644 --- a/include/lsort.h +++ b/include/lsort.h @@ -1,6 +1,8 @@ #ifndef LSORT_H #define LSORT_H +#include "types.h" +int check_sorted(lsort_array_i* array); #endif \ No newline at end of file diff --git a/include/radix.h b/include/radix.h new file mode 100644 index 0000000..144a6a5 --- /dev/null +++ b/include/radix.h @@ -0,0 +1,8 @@ +#ifndef LSORT_RADIX_H +#define LSORT_RADIX_H + +#include "types.h" + +int lsort_radix(lsort_array_i* array, lsort_array_i* return_array); + +#endif \ No newline at end of file diff --git a/include/types.h b/include/types.h index 22ceafb..8506bd3 100644 --- a/include/types.h +++ b/include/types.h @@ -15,20 +15,24 @@ typedef struct { int* items; } lsort_array_i; -lsort_array_i* lsort_create_int_array(); + +lsort_array_i* lsort_array_i_create(); +void lsort_array_i_destroy(lsort_array_i* array); + void lsort_array_i_append(lsort_array_i* array, int number); int lsort_array_i_swap(lsort_array_i* array, int indexA, int indexB); +void lsort_array_i_print(lsort_array_i* array); + + typedef struct { size_t count; size_t capacity; lsort_array_i** items; } lsort_array_i_array; -// struct lsort_int_link { -// int value; -// lsort_array_i* next; -// }; +void lsort_array_i_array_append(lsort_array_i_array* array, lsort_array_i* item); +void lsort_array_i_array_destroy(lsort_array_i_array* array); #endif \ No newline at end of file diff --git a/lsort.c b/lsort.c index ec6fdf4..c34fdbc 100644 --- a/lsort.c +++ b/lsort.c @@ -1,5 +1,12 @@ #include "./include/lsort.h" -int add(int x, int y) { - return x + y; +int check_sorted(lsort_array_i* array) { + if (array->count <= 0) return 2; + for (int i = 0; i < array->count - 1; i++) { + if (array->items[i] > array->items[i + 1]) { + return 1; + }; + } + + return 0; } \ No newline at end of file diff --git a/radix.c b/radix.c new file mode 100644 index 0000000..7784991 --- /dev/null +++ b/radix.c @@ -0,0 +1,79 @@ +#include "include/types.h" +#include "include/radix.h" + +int _seperate_pos_neg(lsort_array_i* array, lsort_array_i* pos_array, lsort_array_i* neg_array) { + for (int i = 0; i < array->count; i++) { + if ((0x80000000 & array->items[i]) == 0x80000000) { + lsort_array_i_append(neg_array, array->items[i]); + } else { + lsort_array_i_append(pos_array, array->items[i]); + } + } + return 0; +} + +int _sort_items(lsort_array_i* numbers, lsort_array_i_array* buckets) { + int byte_check = 0x0000000F; + + for (int i = 0; i < 8; i++) { + // Loop through the list of numbers and append them to their respective buckets for this 4-byte chunk + for (int item_index = 0; item_index < numbers->count; item_index++) { + int* current_number = &numbers->items[item_index]; + lsort_array_i* bucket = buckets->items[(*current_number >> (i * 4)) & byte_check]; + + lsort_array_i_append(bucket, *current_number); + } + + numbers->count = 0; + + // Add the bucketed numbers back to the original array after emptying it + for (int bucket_number = 0; bucket_number < buckets->count; bucket_number++) { + lsort_array_i* bucket = buckets->items[bucket_number]; + + for (int bucket_item = 0; bucket_item < bucket->count; bucket_item++) { + lsort_array_i_append(numbers, bucket->items[bucket_item]); + } + + bucket->count = 0; + } + + // Repeat for each 4-byte chunk... + } + + return 0; +} + +int lsort_radix(lsort_array_i* array, lsort_array_i* return_array) { + if (array->count <= 0) return 1; + + // Seperate positive and negative numbers + lsort_array_i positive_numbers = {0}; + lsort_array_i negative_numbers = {0}; + + if (_seperate_pos_neg(array, &positive_numbers, &negative_numbers)) return 2; + + // Create buckets + lsort_array_i_array buckets = {0}; + for (int i = 0; i < 16; i++) { + lsort_array_i* bucket = lsort_array_i_create(); + lsort_array_i_array_append(&buckets, bucket); + } + + // Sort each set of numbers using those buckets + _sort_items(&negative_numbers, &buckets); + _sort_items(&positive_numbers, &buckets); + + // Add each array of numbers to the return array + for (int i = 0; i < negative_numbers.count; i++) { + lsort_array_i_append(return_array, negative_numbers.items[i]); + } + + for (int i = 0; i < positive_numbers.count; i++) { + lsort_array_i_append(return_array, positive_numbers.items[i]); + } + + // Destroy the buckets + lsort_array_i_array_destroy(&buckets); + + return 0; +} \ No newline at end of file diff --git a/types.c b/types.c index 556b744..ebf1fd5 100644 --- a/types.c +++ b/types.c @@ -1,5 +1,14 @@ #include "include/types.h" #include +#include + +lsort_array_i* lsort_array_i_create() { + return calloc(1, sizeof(lsort_array_i)); +} + +void lsort_array_i_destroy(lsort_array_i* array) { + free(array); +} void lsort_array_i_append(lsort_array_i* array, int item) { if (array->count >= array->capacity) { @@ -18,4 +27,27 @@ int lsort_array_i_swap(lsort_array_i* array, int indexA, int indexB) { array->items[indexB] = temp; return 0; +} + +void lsort_array_i_print(lsort_array_i* array) { + for (int i = 0; i < array->count; i++) { + printf("%d ", array->items[i]); + } + printf("\n"); +} + +void lsort_array_i_array_append(lsort_array_i_array* array, lsort_array_i* item) { + if (array->count >= array->capacity) { + if (array->capacity == 0) array->capacity = 8; + else array->capacity *= 2; + + array->items = realloc(array->items, array->capacity * sizeof(*array->items)); + } + array->items[array->count++] = item; +} + +void lsort_array_i_array_destroy(lsort_array_i_array* array) { + for (int array_number = 0; array_number < array->count; array_number++) { + lsort_array_i_destroy(array->items[array_number]); + } } \ No newline at end of file