Skip to content

Instantly share code, notes, and snippets.

@hpoit
hpoit / insertionsort.jl
Last active March 15, 2018 00:39
Cormen problem 2.4c - insertion sort wrt number of inversions
julia> function insertionsort_ex(A::Array{T}) where T <: Number
for i in 1:length(A)-1
value = A[i+1]
j = i
while value < A[j] && 1 < j # each iteration eliminates one inversion
A[j+1] = A[j] # running time depends on number of inversions to eliminate, θ(n+d)
j -= 1 # where n = length(A)-1 and d = number of inversions
end
A[j+1] = value
end
@hpoit
hpoit / quicksort.jl
Last active March 13, 2018 23:06
Khan quick sort
# https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Julia
julia> function quicksort_ex!(A, i=1, j=length(A))
if j > i
pivot = A[rand(i:j)] # random element of A
left, right = i, j
while left <= right
while A[left] < pivot # partition
left += 1
end
@hpoit
hpoit / hanoitowers.jl
Last active March 13, 2018 13:54
Khan Hanoi Towers
julia> function hanoi(n, source, dest, spare)
if n == 1
println("Move disk 1 from rod $source to rod $dest")
else n >= 2
hanoi(n - 1, source, spare, dest)
println("Move disk $n from rod $source to rod $dest")
hanoi(n - 1, spare, dest, source)
end
end
hanoi (generic function with 1 method)
@hpoit
hpoit / exp_by_square.jl
Last active March 11, 2018 16:13
Khan recursive power (instead using exponentiation by squaring)
# Exponentiation by squaring is the standard method for modular exponentiation on big numbers in asymmetric cryptography
julia> function expbysquare(b::Int, e::Int)
result = BigInt(1)
bigb = BigInt(b)
while e > 0
if isodd(e)
result *= bigb
end
e >>= 1 # set e to itself shifted by one bit to the right, evaluate the new e after shift
@hpoit
hpoit / palindrome.jl
Last active March 10, 2018 17:25
Khan palindrome (iterative)
julia> function ispalindrome(S::String)
i1 = 1
i2 = length(S)
while i2 > i1
if S[i1] != S[i2]
println(S[i1])
println(S[i2])
return false # `false` is not a variable, that's why `return` is necessary
end
i1 += 1
@hpoit
hpoit / recursive_factorial.jl
Last active March 10, 2018 00:14
Khan recursive factorial with BigInt
julia> function recursive_factorial(n)
# first case (base)
if n == 0
return 1
end
# second case (recursive)
if n >= 1
return n*factorial(big(n - 1))
end
end
@hpoit
hpoit / Iterative_factorial.jl
Last active March 10, 2018 00:14
Khan iterative factorial with BigInt
julia> function iterative_factorial(n)
result = big(1)
for i in range(1, n)
result *= i
end
result
end
iterative_factorial (generic function with 1 method)
julia> iterative_factorial(0)
@hpoit
hpoit / Cormen_232.jl
Last active March 13, 2018 22:49
Cormen_232 - sort and merge
https://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Julia
julia> function mergesort(arr::Vector)
if length(arr) ≤ 1 return arr end
mid = length(arr) ÷ 2
lpart = mergesort(arr[1:mid])
rpart = mergesort(arr[mid+1:end])
rst = similar(arr)
i = ri = li = 1
@inbounds while li ≤ length(lpart) && ri ≤ length(rpart)
@hpoit
hpoit / Cormen_122.jl
Last active February 27, 2018 14:33
Cormen 1.2.2
# for n = 1, `log(2, 1) = 0`, making merge time equal 0
# start with n = 2
julia> n = 2;
julia> insertiontime = 8*n^2;
julia> mergetime = 64*n*log(2,n);
julia> while true
@hpoit
hpoit / Cormen_123.jl
Last active February 26, 2018 23:42
Cormen 1.2.3
julia> i = 1;
julia> runningtime1 = 100i^2;
julia> runningtime2 = 2^i;
julia> while true # is a loop that never terminates, unless there's a break
println(i)
if runningtime1 < runningtime2 # if condition is true, break
break