Skip to content

Instantly share code, notes, and snippets.

View kbendick's full-sized avatar
🏥
Recovering from surgery

Kyle Bendickson kbendick

🏥
Recovering from surgery
View GitHub Profile
@kbendick
kbendick / mergesort.py
Created March 8, 2016 20:11
Merge sort using indexing into arrays to run in O(n log n)
def mergesort(L):
return L if len(L) < 2 else merge(mergesort(L[:len(L)/2]), mergesort(L[len(L)/2:]))
def merge(A,B):
Out = []
iA = iB = 0
while len(A)+len(B) > iA + iB:
if len(B) <= iB or (len(A) > iA and A[iA] < B[iB]):
Out.append(A[iA])
iA = iA + 1
@kbendick
kbendick / quicksort.py
Created March 8, 2016 20:08
Simple implementation of quicksort in python
import random
R = random.Random(42)
def qsort(L):
if len(L) < 2: return L
i = R.randrange(len(L)) # Just some random index in the list.
return qsort([x for x in L if x < L[i]]) + [x for x in L if x == L[i]] + qsort([x for x in L if x > L[i]])
@kbendick
kbendick / radix_sort.cpp
Last active September 8, 2021 05:46
Implementation of radix sort in c++
#include <queue>
int select_digit (int number, int place)
{ return number/place % 10; }
/*
Create an array of 10 buckets, each storing an empty queue (use ArrayQueue)
for every place (6 digit numbers: 1s, 10s, 100s, 1,000s, 10,000s, and 100,000s)
for every value in array a
@kbendick
kbendick / mergesort.cpp
Created March 8, 2016 19:44
Implementation of merge sort in c++
// Declare/initialize any useful variables here, including the temporary array
// to merge values into and then copy back into the parameter array.
/*
while the temporary array is not filled
if there are no more values in the left part of a,
move next value from right part of a into next index of the temporary array otherwise, if there are no more values in the right part of a,
move next value from left part of a into next index of the temporary array otherwise (values in each part) compare the first value in each part
move smallest value from a into next index of the temporary array