Skip to content

Instantly share code, notes, and snippets.

View maxgoren's full-sized avatar

Max Goren maxgoren

View GitHub Profile
@maxgoren
maxgoren / bottomup-natural-queuesort.c
Last active August 8, 2023 23:48
A bottom up NATURAL mergesort for linked lists.
/*
a very fast bottom up natural mergesort
A full description of the algorithm can be found on
http://www.maxgcoding.com/queue-mergesort-a-comparison-optimal-bottom-up-sort-for-linked-lists/
(C) 2023 Max Goren
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the “Software”), to deal in the Software
@maxgoren
maxgoren / selectionsort.c
Created June 14, 2023 21:29
Selection sort.
/* modest though definite gains over the "text book" implementation
of selection sort. Unfortunately this discovery was overshadowed
by the original authors aloofness. */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void optimizedSelectionSort(int array[], int array_length) {
int current_position, largest_value, comparison_value;
@maxgoren
maxgoren / sort-off.c
Created June 14, 2023 15:22
u/frymimori's sort vs introspective sort
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define CUTOFF 16
void sorterQ(int * b, unsigned long a) {
unsigned long c = a - 1;
unsigned long d = 0;
unsigned long e;
@maxgoren
maxgoren / stilldumb.c
Created June 13, 2023 20:42
its always something
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sorterQ(unsigned long a, int * b) {
unsigned long c = a - 1;
unsigned long d = 0;
unsigned long e;
int f;
@maxgoren
maxgoren / evendumber.c
Last active June 13, 2023 19:43
after claiming his code worked in C but not in C++, here is u/frymimori's code vs shellsort both, in C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sorterQ(unsigned long a, int * b) {
unsigned long c = a;
unsigned long d = 0;
unsigned long e;
int f;
#include <iostream>
#include <chrono>
using namespace std;
void sorterQ(unsigned long a, int * b) {
unsigned long c = a;
unsigned long d = 0;
unsigned long e;
int f;
/*
This highlights the problems with big O notation. This algorithm is
a naiive mergesort implementation. When analyzed its clear to see it is O(nlogn)
however, this implementation takesd over 5 seconds to sort 2.5m int's where
a well done implementation can do it in 1/10th the time.
*/
vector<int> merge(vector<int>& a, vector<int>& b) {
int i = 0, j = 0;
vector<int> res;
@maxgoren
maxgoren / LinkedPartition.cpp
Created April 9, 2023 18:52
Partition a Linked list, pivoting around a value
ListNode* partition(ListNode* head, int x) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode l; ListNode *a = &l;
ListNode r; ListNode *b = &r;
ListNode* p = head;
while (p != nullptr) {
ListNode* t = p;
p = p->next;
t->next = nullptr;
@maxgoren
maxgoren / AVLInsert.cpp
Created February 19, 2023 16:10
A very straight forward Iterative AVL insertion algorithm using parent pointers based on CLRS
template <class T, class T2>
class AVLTree {
private:
struct node {
T key;
T2 value;
int height;
node* left;
node* right;
node* parent;
@maxgoren
maxgoren / SuffixArray.java
Last active February 14, 2023 00:06
toy example of building a suffix array and using it for efficient string search.
import java.util.List;
import java.util.ArrayList;
public class SuffixArray {
private static class Suffix {
public int index;
public String suffix;
Suffix(int i, String s) {
this.index = i;
this.suffix = s;