Skip to content

Instantly share code, notes, and snippets.

View groshart's full-sized avatar
🏠
Working from home

Luther Heggs groshart

🏠
Working from home
  • Omaha, NE
View GitHub Profile
@groshart
groshart / rotate1.c
Created May 7, 2021 15:45
Array rotate methods
// Reversal swap
void reverse(int *arr, int size)
{
for (int lo = 0, hi = size - 1; lo < hi; ++lo, --hi) {
int tmp = arr[lo];
arr[lo] = arr[hi];
arr[hi] = tmp;
}
}
@groshart
groshart / cartesian_product.hpp
Last active May 4, 2021 04:03
Cartesian product of a 2D container
namespace
{
template <typename FwdIt, typename Outer, typename Inner>
void cartesian_product(FwdIt first, FwdIt last, Outer &output, Inner &current)
{
if (first == last) {
output.push_back(current);
return;
}
for (const auto &value : *first) {
@groshart
groshart / merge_sort.hpp
Last active December 23, 2024 17:02
Simple merge sort
#pragma once
#include <algorithm>
#include <iterator>
#include <utility>
#include <vector>
namespace msort
{
enum { SMALL_CUTOFF = 16 };
@groshart
groshart / memswap.c
Created April 13, 2021 09:12
C - generic swap function
#include <stddef.h>
#include <stdint.h>
#include <string.h>
typedef uint64_t word;
void memswap(void *a, void *b, size_t size)
{
char *x = a;
char *y = b;
@groshart
groshart / making_shellsort.cpp
Created March 28, 2021 04:44
From insertion sort to Shellsort
namespace v1 // original insertion sort
{
void insertion_sort(int *arr, int count)
{
for (int i = 1; i < count; ++i) {
int key = arr[i];
int j = i;
for (; j > 0 && key < arr[j - 1]; --j) {
arr[j] = arr[j - 1];
}
@groshart
groshart / insertion_sort.cpp
Last active April 16, 2021 05:01
Insertion sort variations
#include <algorithm>
#include <functional>
#include <utility>
void insertion_sort(int *arr, int count) // "j-1" indexing
{
for (int i = 1; i < count; ++i) {
int key = arr[i];
int j = i;
for (; j > 0 && key < arr[j - 1]; --j) {
@groshart
groshart / cocktail_sort.cpp
Created February 18, 2021 21:52
Cocktail shaker sort
#include <utility>
using std::swap;
// optimization: remember last swap index after each scan to reduce sorting range
void cocktail_sort(int *arr, int count)
{
int lo = 0, hi = count - 1, last = lo;
while (lo < hi) {
for (int i = lo; i < hi; ++i) {
@groshart
groshart / rotate.hpp
Last active May 7, 2021 15:49
Gries-Mills block swap (rotate)
/*
Swapping Sections
Gries, David; Mills, Harlan
1981-01
Cornell University
https://ecommons.cornell.edu/handle/1813/6292
*/
#include <algorithm>
@groshart
groshart / quicksort.cpp
Created July 12, 2020 00:05
Quicksort (logN stack, Hoare partition, median of 3, insertion sort small partitions)
#include <utility>
namespace
{
using std::swap;
enum { CUTOFF = 16 }; // cutoff size for insertion sort
void insertion_sort(int *arr, int size) // insertion sort small partitions
{
for (int i = 1; i < size; ++i) {
@groshart
groshart / shellsort.cpp
Last active March 11, 2021 09:46
Shellsort gap sequences
// Shellsort
namespace shell
{
// insertion sort with gap length h
void h_sort(int *arr, int count, int h = 1)
{
for (int i = h; i < count; ++i) {
int key = arr[i];
int j = i;