-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmin_max.rb
More file actions
50 lines (48 loc) · 937 Bytes
/
Copy pathmin_max.rb
File metadata and controls
50 lines (48 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module MedianAndOrderStatistics
class << self
# Public: Returns the minimum number in an array.
#
# ARGS:
# a - Input array
#
# RETURN: Number
# NOTE: Ruby in-built method is array.min
#
# COMPLEXITY: Θ(n)
#
# Examples
# minimum([4, 1, 3, 2, 16, 9, 10, 14, 18, 7])
# => 1
def minimum(a)
min = a[0]
(1..a.length-1).each do |i|
if a[i] < min
min = a[i]
end
end
min
end
# Public: Returns the maximum number in an array.
#
# ARGS:
# a - Input array
#
# RETURN: Number
# NOTE: Ruby in-built method is array.max
#
# COMPLEXITY: Θ(n)
#
# Examples
# maximum([4, 1, 3, 2, 16, 9, 10, 14, 18, 7])
# => 18
def maximum(a)
max = a[0]
(1..a.length-1).each do |i|
if a[i] > max
max = a[i]
end
end
max
end
end
end