Skip to content

Commit 3c4032d

Browse files
Merge branch 'main' into directory_writer
2 parents 50edbd2 + ede895a commit 3c4032d

File tree

8 files changed

+90
-51
lines changed

8 files changed

+90
-51
lines changed

src/searches/binary_search.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@ function binary_search(
6767
) where {T<:Real}
6868
if (r >= l)
6969
mid = Int(ceil(l + (r - l) / 2))
70-
# println(mid)
7170
if (arr[mid] == x)
72-
return "Element present at index $mid"
71+
return mid
7372
elseif (arr[mid] > x)
7473
binary_search(arr, l, mid - 1, x)
7574
else
7675
binary_search(arr, mid + 1, r, x)
7776
end
7877
else
79-
return "Element not present in array"
78+
return -1
8079
end
8180
end

src/searches/exponential_search.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Contributed By:- [Ash](https://github.com/ashwani-rathee)
1919
function exponential_search(arr::AbstractArray{T,1}, x::T) where {T<:Real}
2020
n = size(arr)[1]
2121
if (arr[1] == x)
22-
return "Element present at index 1"
22+
return 1
2323
end
2424

2525
i = 1

src/searches/interpolation_search.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ function interpolation_search(
4646
if (r >= l && x >= arr[l] && x <= arr[r])
4747
mid = Int(ceil(l + (((x - arr[l]) * (r - l)) / (arr[r] - arr[l]))))
4848
if (arr[mid] == x)
49-
return "Element present at index $mid"
49+
return mid
5050
elseif (arr[mid] > x)
5151
interpolation_search(arr, l, mid - 1, x)
5252
else
5353
interpolation_search(arr, mid + 1, r, x)
5454
end
5555
else
56-
return "Element not present in array"
56+
return -1
5757
end
5858
end

src/sorts/heap_sort.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ After the largest element has been extracted, the tree is updated to maintain th
2828
Contributed By:- [Frank Schmitt](https://github.com/frankschmitt)
2929
"""
3030
function heap_sort!(arr::Vector{T}, gt = >, N::Int = length(arr)) where {T}
31+
if isempty(arr)
32+
return
33+
end
3134
n = N
3235
i = div(n, 2)
3336
t = -1

test/cipher.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ using TheAlgorithms.Cipher
1010
end
1111

1212
@testset "Cipher: caesar" begin
13-
input = "stealth"
14-
@test caesar(0, input) == "stealth"
15-
input = "ghsozhv"
16-
@test caesar(90, input) == "stealth"
13+
@test caesar(0, "stealth") == "stealth"
14+
@test caesar(90, "ghsozhv") == "stealth"
15+
16+
@test caesar(1, "ABC") == "BCD"
17+
@test caesar(-1, "BCD") == "ABC"
18+
19+
@test caesar(2, "f(x)") == "h(z)"
20+
21+
@test caesar(5, 'a') == 'f'
1722

1823
# rot = 13
1924
# s = "abcdefghijklmnopqrstuvwxyz"

test/math.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ using TheAlgorithms.Math
44
@test abs_val(-100) == 100
55
@test abs_val(0) == 0
66
@test abs(123.1) == 123.1
7-
@test (-1000 == abs_val(-1000)) == false
8-
@test (1000 == abs_val(1000)) == true
7+
@test abs_val(-1000) == 1000
8+
@test abs_val(1000) == 1000
99

1010
@test abs_max([1, 3, 4]) == 4
1111
@test abs_max([-3, 1, 2]) == -3

test/searches.jl

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,58 @@ using TheAlgorithms.Searches
1616
# Second method used for exponential search
1717
arr = [1, 2, 3, 4, 13, 15, 20]
1818
# The next three values used are the result of the while-loop inside `exponential search`
19-
@test binary_search(arr, 4, 7, 4) == "Element present at index 4"
20-
@test binary_search(arr, 4, 7, 10) == "Element not present in array"
19+
@test binary_search(arr, 4, 7, 4) == 4
20+
@test binary_search(arr, 4, 7, 20) == 7
21+
@test binary_search(arr, 4, 7, 10) == -1
22+
@test binary_search(arr, 4, 7, 21) == -1
2123
end
2224
end
2325

2426
@testset "Searches: Linear" begin
25-
array = [1, 3, 4, 7, 8, 11]
26-
linear_search(array, 3)
27-
linear_search(array, 8)
28-
linear_search(array, 12)
27+
array = [1, 30, 4, 70, 8, 11]
28+
@test linear_search(array, 1) == 1
29+
@test linear_search(array, 30) == 2
30+
@test linear_search(array, 8) == 5
31+
@test linear_search(array, 11) == 6
32+
@test linear_search(array, 12) == -1
33+
@test linear_search(array, 10) == -1
34+
@test linear_search(array, 0) == -1
2935
end
3036

3137
@testset "Searches: Exponential" begin
3238
arr = [1, 2, 3, 4, 13, 15, 20]
33-
x = 4
34-
n = size(arr)[1]
35-
l = 1
36-
r = n
37-
@test exponential_search(arr, 1) == "Element present at index 1"
38-
@test exponential_search(arr, x) == "Element present at index 4"
39-
@test exponential_search(arr, 100) == "Element not present in array"
39+
@test exponential_search(arr, 1) == 1
40+
@test exponential_search(arr, 4) == 4
41+
@test exponential_search(arr, 20) == 7
42+
@test exponential_search(arr, 5) == -1
43+
@test exponential_search(arr, 100) == -1
4044
end
4145

4246
@testset "Searches: Interpolation" begin
4347
arr = [1, 2, 3, 4, 13, 15, 20]
44-
x = 15
45-
n = size(arr)[1]
4648
l = 1
47-
r = n
49+
r = size(arr)[1]
4850

49-
interpolation_search(arr, l, r, x)
51+
@test interpolation_search(arr, l, r, 1) == 1
52+
@test interpolation_search(arr, l, r, 2) == 2
53+
@test interpolation_search(arr, l, r, 3) == 3
54+
@test interpolation_search(arr, l, r, 4) == 4
55+
@test interpolation_search(arr, l, r, 13) == 5
56+
@test interpolation_search(arr, l, r, 15) == 6
57+
@test interpolation_search(arr, l, r, 20) == 7
58+
@test interpolation_search(arr, l, r, 5) == -1
59+
@test interpolation_search(arr, l, r, 14) == -1
60+
@test interpolation_search(arr, l, r, 19) == -1
61+
@test interpolation_search(arr, l, r, 0) == -1
62+
@test interpolation_search(arr, l, r, 1000) == -1
5063
end
5164

5265
@testset "Searches: Jump" begin
53-
arr = [1, 2, 3, 31, 4, 13]
54-
x = 31
55-
jump = 2 # optimum is sqroot(n)
56-
n = size(arr)[1]
57-
58-
jump_search(arr, x, jump)
66+
arr = [1, 2, 3, 4, 13, 31]
67+
@test jump_search(arr, 31, 1) == 6
68+
@test jump_search(arr, 13, 3) == 5
69+
@test jump_search(arr, 3, 5) == 3
70+
@test jump_search(arr, 1, 3) == 1
71+
@test jump_search(arr, 10000, 3) == -1
5972
end
6073
end

test/sorts.jl

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
using TheAlgorithms.Sorts
22

33
@testset "Sorts" begin
4-
sorts = [
5-
bogo_sort!
6-
bubble_sort!
7-
bucket_sort!
8-
counting_sort!
9-
exchange_sort!
10-
heap_sort!
11-
insertion_sort!
12-
merge_sort!
13-
quick_sort!
14-
selection_sort!
15-
]
4+
@testset "bogo_sort" begin
5+
x = [3, 1, 4, 2]
6+
bogo_sort!(x)
7+
@test x == [1, 2, 3, 4]
8+
end
9+
10+
@testset "deterministic sorts" begin
11+
sorts = [
12+
bubble_sort!
13+
bucket_sort!
14+
counting_sort!
15+
exchange_sort!
16+
heap_sort!
17+
insertion_sort!
18+
merge_sort!
19+
quick_sort!
20+
selection_sort!
21+
]
22+
23+
for f in sorts
24+
x = [3, 5, 1, 4, 2]
25+
f(x)
26+
@test x == [1, 2, 3, 4, 5]
27+
28+
y = [5, 2, 2, 2, 1, 5, 2, 34, 6, 4, 3, 2, 1, 2, 3, 4, 10, 1]
29+
f(y)
30+
@test y == [1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 10, 34]
31+
32+
z = []
33+
f(z)
34+
@test z == []
1635

17-
for f in sorts
18-
x = [3, 5, 1, 4, 2]
19-
f(x)
20-
@test x == [1, 2, 3, 4, 5]
36+
s = [1]
37+
f(s)
38+
@test s == [1]
39+
end
2140
end
2241
end

0 commit comments

Comments
 (0)