Compare commits

...

5 Commits

Author SHA1 Message Date
de5493e7c6 Implement QuickSort 2026-05-08 04:33:59 -05:00
b6b2500944 add missed radix sort tests 2026-05-06 05:47:11 -05:00
9b56a240d1 radix sort pog 2026-05-06 05:46:50 -05:00
c1efc67106 rename types 2026-05-05 01:30:10 -05:00
cb5111de82 add array functionality 2026-05-05 01:21:11 -05:00
10 changed files with 362 additions and 14 deletions

View File

@@ -7,7 +7,7 @@ CC = gcc
SHELL := /bin/bash SHELL := /bin/bash
INCLUDE := include INCLUDE := include
CFLAGS = -Wall -I$(INCLUDE) CFLAGS = -Wall -I$(INCLUDE) -ggdb
BUILD_DIR := build BUILD_DIR := build
@@ -18,8 +18,14 @@ INCLUDE_INSTALL_PATH := /usr/local/include/lsort
LIB_INSTALL_PATH := /usr/local/lib LIB_INSTALL_PATH := /usr/local/lib
STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \ STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \
$(STATIC_BUILD_DIR)/types.o \
$(STATIC_BUILD_DIR)/radix.o \
$(STATIC_BUILD_DIR)/quicksort.o
DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \ DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \
$(STATIC_BUILD_DIR)/types.o \
$(STATIC_BUILD_DIR)/radix.o \
$(STATIC_BUILD_DIR)/quicksort.o
TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \ TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \
@@ -43,21 +49,24 @@ static: $(STATIC_BUILD_FILES)
$(STATIC_BUILD_DIR)/%.o: %.c | $(STATIC_BUILD_DIR) $(STATIC_BUILD_DIR)/%.o: %.c | $(STATIC_BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
test: dynamic static $(TEST_BUILD_FILES) test: $(TEST_BUILD_FILES) test_dynamic test_static
$(CC) $(CFLAGS) -L$(CURDIR)/$(DYNAMIC_BUILD_DIR) -llsort -o $(TEST_BUILD_DIR)/test_dynamic $(TEST_BUILD_FILES)
$(CC) $(CFLAGS) -o $(TEST_BUILD_DIR)/test_static $(TEST_BUILD_FILES) $(STATIC_BUILD_DIR)/liblsort.a test_dynamic: dynamic $(TEST_BUILD_FILES)
$(CC) $(CFLAGS) -L$(CURDIR)/$(DYNAMIC_BUILD_DIR) -llsort -o $(TEST_BUILD_DIR)/test_dynamic $(TEST_BUILD_FILES)
@echo "" @echo ""
@echo "Running dynamic test..." @echo "Running dynamic test..."
@echo "" @echo ""
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/build/dynamic ./$(TEST_BUILD_DIR)/test_dynamic LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/build/dynamic ./$(TEST_BUILD_DIR)/test_dynamic
test_static: static $(TEST_BUILD_FILES)
$(CC) $(CFLAGS) -o $(TEST_BUILD_DIR)/test_static $(TEST_BUILD_FILES) $(STATIC_BUILD_DIR)/liblsort.a
@echo "" @echo ""
@echo "Running static test..." @echo "Running static test..."
@echo "" @echo ""
./$(TEST_BUILD_DIR)/test_static ./$(TEST_BUILD_DIR)/test_static
$(TEST_BUILD_DIR)/%.o: test/%.c | $(TEST_BUILD_DIR) $(TEST_BUILD_DIR)/%.o: test/%.c | $(TEST_BUILD_DIR)

View File

@@ -1,6 +1,8 @@
#ifndef LSORT_H #ifndef LSORT_H
#define LSORT_H #define LSORT_H
int add(int x, int y); #include "types.h"
int check_sorted(lsort_array_i* array);
#endif #endif

8
include/quicksort.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef LSORT_QUICKSORT_H
#define LSORT_QUICKSORT_H
#include "types.h"
int lsort_quicksort(lsort_array_i* array, int (*function_pointer)(int, int));
#endif

8
include/radix.h Normal file
View File

@@ -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

38
include/types.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef LSORT_TYPES_H
#define LSORT_TYPES_H
#include <stddef.h>
#include "string.h"
typedef struct {
const char *buf;
size_t len;
} lsort_string;
typedef struct {
size_t count;
size_t capacity;
int* items;
} lsort_array_i;
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;
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

11
lsort.c
View File

@@ -1,5 +1,12 @@
#include "./include/lsort.h" #include "./include/lsort.h"
int add(int x, int y) { int check_sorted(lsort_array_i* array) {
return x + y; 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;
} }

45
quicksort.c Normal file
View File

@@ -0,0 +1,45 @@
#include "include/lsort.h"
#include "include/types.h"
#include "include/quicksort.h"
#include <stdio.h>
typedef int (*int_function_pointer)(int, int);
static int default_compare_function(int a, int b) {
return a - b;
}
static int partition(lsort_array_i* array, int L, int R, int_function_pointer compare_function) {
int pivot = array->items[R];
int i = L - 1;
for (int j = L; j < R; j++) {
if (compare_function(array->items[j], pivot) < 0) {
i = i + 1;
lsort_array_i_swap(array, i, j);
}
}
lsort_array_i_swap(array, i + 1, R);
return i + 1;
}
static void _quicksort(lsort_array_i* array, int L, int R, int_function_pointer compare_function) {
if (L < R) {
int pivotIndex = partition(array, L, R, compare_function);
_quicksort(array, L, pivotIndex - 1, compare_function);
_quicksort(array, pivotIndex + 1, R, compare_function);
}
}
int lsort_quicksort(lsort_array_i* array, int_function_pointer compare_function) {
if (array->count < 1) {
return 0;
}
if (compare_function == NULL) compare_function = default_compare_function;
_quicksort(array, 0, (int)array->count - 1, compare_function);
return check_sorted(array) ? 1 : 0;
}

79
radix.c Normal file
View File

@@ -0,0 +1,79 @@
#include "include/types.h"
#include "include/radix.h"
static 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;
}
static 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;
}

View File

@@ -1,11 +1,110 @@
#include "../include/types.h"
#include "../include/radix.h"
#include "../include/quicksort.h"
#include "../include/lsort.h" #include "../include/lsort.h"
#include "stdio.h" #include <stdio.h>
int main(void) { int test_array_i_append(lsort_array_i* array) {
int x = 5; printf("lsort_array_i_append()...\n");
int y = 2;
printf("%d + %d = %d\n", x, y, add(x, y)); lsort_array_i_append(array, 25);
lsort_array_i_append(array, 64);
lsort_array_i_append(array, 34);
lsort_array_i_append(array, 74);
lsort_array_i_append(array, 99);
lsort_array_i_append(array, 23);
if (array->items[5] != 23) return 1;
return 0; return 0;
} }
int test_array_i_swap(lsort_array_i* array) {
printf("lsort_array_i_swap()...\n");
lsort_array_i_swap(array, 0, 5);
lsort_array_i_swap(array, 4, 2);
lsort_array_i_swap(array, 2, 3);
if (array->items[0] != 23 || array->items[5] != 25 || array->items[4] != 34 || array->items[2] != 74 || array->items[3] != 99) return 1;
return 0;
}
int test_append_swap() {
printf("test_append_swap()...\n");
lsort_array_i array_append_swap = {0};
if (test_array_i_append(&array_append_swap)) return 1;
if (test_array_i_swap(&array_append_swap)) return 1;
lsort_array_i array_sort_check = {0};
lsort_array_i_append(&array_sort_check, 1);
lsort_array_i_append(&array_sort_check, 5);
lsort_array_i_append(&array_sort_check, 7);
lsort_array_i_append(&array_sort_check, 8);
lsort_array_i_append(&array_sort_check, 15);
lsort_array_i_append(&array_sort_check, 28);
if (check_sorted(&array_sort_check) == 1) return 1;
lsort_array_i_swap(&array_sort_check, 2, 4);
lsort_array_i_swap(&array_sort_check, 0, 3);
if (check_sorted(&array_sort_check) == 0) return 1;
return 0;
}
int test_radix_sort() {
printf("lsort_radix()...\n");
lsort_array_i array_radix = {0};
lsort_array_i_append(&array_radix, -2);
lsort_array_i_append(&array_radix, -15);
lsort_array_i_append(&array_radix, 25);
lsort_array_i_append(&array_radix, 56);
lsort_array_i_append(&array_radix, 23);
lsort_array_i_append(&array_radix, -222);
lsort_array_i_append(&array_radix, -58);
lsort_array_i array_radix_return = {0};
lsort_radix(&array_radix, &array_radix_return);
if (check_sorted(&array_radix_return) != 0) return 1;
return 0;
}
int test_quicksort() {
printf("lsort_quicksort()...\n");
lsort_array_i array_quicksort = {0};
lsort_array_i_append(&array_quicksort,6);
lsort_array_i_append(&array_quicksort, 5);
lsort_array_i_append(&array_quicksort, 4);
lsort_array_i_append(&array_quicksort, 3);
lsort_array_i_append(&array_quicksort, 2);
lsort_array_i_append(&array_quicksort, 5);
lsort_array_i_append(&array_quicksort, 1);
lsort_array_i_append(&array_quicksort, 0);
lsort_quicksort(&array_quicksort, NULL);
if (check_sorted(&array_quicksort)) return 1;
return 0;
}
int main(void) {
if (
test_append_swap() ||
test_radix_sort() ||
test_quicksort()
) return 1;
printf("\nTest completed successfully.\n");
return 0;
}

53
types.c Normal file
View File

@@ -0,0 +1,53 @@
#include "include/types.h"
#include <stdlib.h>
#include <stdio.h>
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) {
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;
}
int lsort_array_i_swap(lsort_array_i* array, int indexA, int indexB) {
if (indexA > array->count - 1 || indexB > array->count - 1) return 1;
int temp = array->items[indexA];
array->items[indexA] = array->items[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]);
}
}