This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |