Skip to content

Instantly share code, notes, and snippets.

View bottomupmergesort's full-sized avatar
💭
Coding

Max Goren bottomupmergesort

💭
Coding
View GitHub Profile
@bottomupmergesort
bottomupmergesort / LLRB-TreeSort.cpp
Last active June 28, 2022 20:21
An interesting worst-case O(N log N) sort.
//LLRB-TreeSort.cpp - Max Goren 6/28/22
//
// TreeSort utilizing Left Leaning Red Black Tree
// the algorithm's input is an array to be sorted, output is the sorted array.
// TreeSort exploits a binary search tree's ordered property in that an in-order
// traversal yields the items in sorted order. This implementation doubles down
// with the use of a Left Leaning Red Black Tree to keep the process of building
// the binary search tree guranteed O(N log N). the traversal itsself is an O(n) operation,
// storing each nodes item in the T* sorted array.
//
@bottomupmergesort
bottomupmergesort / llms.c
Created April 29, 2022 15:18
Mergesort a linked list
void merge_sort(struct node** h)
{
if (*h == NULL || (*h)->next == NULL)
return;
struct node *fast = (*h)->next, *slow = *h;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
void ht_delete(hash_table* ht, char* key)
{
unsigned int index = hash_function(key, ht->tableSize);
//first check if its the first slot, as we are using different header structs than link structs
if (strcmp(ht->table[index]->next->key, key) == 0)
{
ht_item* temp = ht->table[index]->next;
ht->table[index]->next = ht->table[index]->next->next;
free(temp);
return;
@bottomupmergesort
bottomupmergesort / scsearch.c
Created March 9, 2022 21:11
data retrieval in sc hash table
char* ht_search(hash_table* ht, char* key)
{
unsigned int index = hash_function(key, ht->tableSize);
for (ht_item* itr = ht->table[index]->next; itr != NULL; itr = itr->next)
if (strcmp(itr->key, key) == 0)
return itr->value;
return NULL;
}
@bottomupmergesort
bottomupmergesort / scinsert.c
Created March 9, 2022 21:08
insertion in separate chaining hash table
void ht_insert(hash_table* ht, char* key, char* info)
{
unsigned int index = hash_function(key, ht->tableSize);
ht_item* newItem = make_item(key, info);
newItem->next = ht->table[index]->next;
ht->table[index]->next = newItem;
ht->table[index]->slotCount += 1;
ht->count += 1;
}
@bottomupmergesort
bottomupmergesort / seperatechainingds.c
Created March 9, 2022 20:50
foundational ds for a separate chained hash table
typedef struct ht_item {
char* key;
char* value;
struct ht_item* next;
} ht_item;
ht_item* make_item(char* key, char* value)
{
ht_item* newItem = (ht_item*)malloc(sizeof(ht_item));
newItem->key = (char*)malloc(sizeof(char) * strlen(key));
@bottomupmergesort
bottomupmergesort / linearprobesearch.c
Last active March 9, 2022 19:48
searching via linear probing in hash table
char* search(hash_table* ht, char* key)
{
unsigned int index = hash_function(key);
while (index < ht->tableSize && ht->table[index] != NULL)
{
if (strcmp(ht->table[index]->key, key) == 0)
{
return ht->table[index]->value;
} else {
index = (index + 1) % ht->tableSize;
@bottomupmergesort
bottomupmergesort / htinsert.c
Last active March 9, 2022 19:51
Open Address insertion
void insert(hash_table* ht, char* key, char* value)
{
if (ht->tableSize == ht->count + 1)
{
printf("Table Overflow: Hash Table Full!");
return 0;
}
unsigned int tableIndex = hash_function(key, ht->tableSize);
if (ht->table[tableIndex] == NULL)
{
@bottomupmergesort
bottomupmergesort / table.c
Created March 8, 2022 22:36
hashtable structure
typedef struct hash_table {
ht_item** table;
int count;
int tableSize;
} hash_table;
hash_table* initHashTable(hash_table* ht, int table_size)
{
ht = (hash_table*)malloc(sizeof(hash_table));
ht->tableSize = table_size;
@bottomupmergesort
bottomupmergesort / tablebucket.c
Last active March 9, 2022 00:41
Hash Table Bucket
typedef struct ht_item_t {
char* key;
char* value;
} ht_item;
ht_item* make_item(char* key, char* value)
{
ht_item* nhti = (ht_item*)malloc(sizeof(ht_item));
nhti->key = (char*)malloc(sizeof(char) * strlen(key));
nhti->value = (char*)malloc(sizeof(char) * strlen(value));