Top Level Namespace

Defined in:

Method Summary

Method Detail

def binary_search(arr : Array(U), left : Int32, right : Int32, value : U) forall U #

[View source]
def binary_search(arr : Array(U), value : U) forall U #

[View source]
def bubble_sort(arr : Array(U)) forall U #

Optimized version of the bubble sort algorithm. The standard algorithm always runs in O(n^2) time even if the array is sorted. This one will stop running if it realizes that the array is already sorted.


[View source]
def heap_sort(arr : Array(U)) forall U #

[View source]
def insertion_sort(arr : Array(U)) forall U #

Crystal implementation of the insertion sort algorithm. This algorithm always has a time complexity of O(n^2) so it's certainly not the most efficient, but it is super easy to implement.


[View source]
def make_heap(arr : Array(U), n : Int32, i : Int32) forall U #

Make a subtree into a heap of size n rooted at index i.


[View source]
def merge_sort(arr : Array(U)) forall U #

Crystal implementation of the merge sort algorithm, which divides an array into 2 halves and takes a divide and conquer approach. Time complexity of this algorithm isn't great, since it always splits the array regardless of size, sorts them individually (and not in parallel), and then takes linear time to merge them back together.


[View source]