forked from AdaGold/restricted-arrays
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathusing_restricted_array.rb
More file actions
236 lines (207 loc) · 6.7 KB
/
using_restricted_array.rb
File metadata and controls
236 lines (207 loc) · 6.7 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require_relative 'restricted_array.rb'
# RestrictedArray can be created using a specified size, or a random size in
# the range of 1-20 will be chosen for you.
# All values are integers in the range of 1-221.
# RestrictedArray cannot be resized.
############################################################################
# Calculates the length of the restricted array. All values are integers.
# The restricted_array is terminated by 'nil' i.e. array[length] = nil
# Time complexity: O(n); requires n steps or n operations where n is the length of the array
# Space complexity: O(n); requires n steps or n operations where n is the length of the array
def length(array)
i = 0
while array[i] != nil
i += 1
end
return i
end
# DELETE AFTER TESTING
puts "testing length method"
test_arr = RestrictedArray.new(3)
pp test_arr
p length(test_arr)
puts ""
############################################################################
# Prints each integer values in the array
# Time complexity: O(n); requires n steps or n operations where n is the length of the array
# Space complexity: O(n); requires n steps or n operations where n is the length of the array
def print_array(array)
i = 0
while array[i] != nil
puts "#{array[i]}"
i += 1
end
end
# DELETE AFTER TESTING
puts "testing print_arr method"
test_arr = RestrictedArray.new(3)
pp test_arr
p print_array(test_arr)
puts ""
############################################################################
# For an unsorted array, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: O(n); requires n steps or n operations where n is the length of the array
# Space complexity: O(n); requires n steps or n operations where n is the length of the array
def search(array, length, value_to_find)
if length == 0
return nil
end
i = 0
while i < length
if array[i] == value_to_find
return true
end
i += 1
end
return false
end
# DELETE AFTER TESTING
puts "testing search method"
test_arr = [188, 45, 129]
pp test_arr
p search(test_arr, 3, 129)
puts ""
############################################################################
# Finds and returns the largest integer value the array
# Assumes that the array is not sorted.
# Time complexity: O(n); requires n steps or n operations where n is the length of the array
# Space complexity: O(n); requires n steps or n operations where n is the length of the array
def find_largest(array, length)
if length == 0
return nil
end
i = 0
largest_value = array[0]
while i < length
if array[i] > largest_value
largest_value = array[i]
end
i += 1
end
return largest_value
end
# DELETE AFTER TESTING
puts "testing find_largest method"
test_arr = [188, 45, 189]
pp test_arr
p find_largest(test_arr, 3)
puts ""
############################################################################
# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: O(n); requires n steps or n operations where n is the length of the array
# Space complexity: O(n); requires n steps or n operations where n is the length of the array
def find_smallest(array, length)
if length == 0
return nil
end
i = 0
smallest_value = array[0]
while i < length
if array[i] < smallest_value
smallest_value = array[i]
end
i += 1
end
return smallest_value
end
# DELETE AFTER TESTING
puts "testing smallest method"
test_arr = [188, 45, 129]
pp test_arr
p find_smallest(test_arr, 3)
puts ""
############################################################################
# Reverses the values in the integer array in place
# Time complexity: O(n); requires n steps or n operations where n is the length of the array
# Space complexity: O(n); requires n steps or n operations where n is the length of the array
def reverse(array, length)
if length <= 1
return nil
end
i = 0
j = (length - 1)
while i < j
temp = array[i]
array[i] = array[j]
array[j] = temp
i += 1
j -= 1
end
return array
end
puts "testing reverse method"
test_arr = [188, 45, 129]
pp test_arr
p reverse(test_arr, 3)
puts ""
############################################################################
# For an array sorted in ascending order, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: O(logn)
# Space complexity: O(logn)
def binary_search(array, length, value_to_find)
if length == 0
return false
end
#
low = 0
high = (length - 1)
mid = 0
while low <= high
mid = (low + high) / 2
if array[mid] == value_to_find
# // compare the element at index mid with the value to find
return true
elsif array[mid] > value_to_find
# // value to find is less than the value at mid index
# // eliminate the second half
high = (mid - 1)
elsif array[mid] < value_to_find
# // value to find is greater than the value at mid index
# // eliminate the first half
low = (mid + 1)
end
end
# // value not found in the array
return false
end
puts "testing binary_search method"
test_arr = [188, 45, 129]
pp test_arr
p binary_search(test_arr, 3, 129)
puts ""
############################################################################
# Helper method provided to sort the array in ascending order
# Implements selection sort
# Time complexity = O(n^2), where n is the number of elements in the array.
# To find the correct value for index 0, a total of (n-1) comparisons are needed.
# To find the correct value for index 1, a total of (n-2) comparisons are needed.
# To find the correct value for index 2, a total of (n-3) comparisons are needed.
# and so on ...
# To find the correct value for index (n-2), a total of 1 comparisons is needed.
# To find the correct value for the last index, a total of 0 comparisons are needed.
# Total number of comparisons = (n-1) + (n-2) + ... 3 + 2 + 1
# = (n * (n-1))/2
# This is O(n^2) in Big O terms.
# Space complexity = constant or O(1) since the additional storage needed,
# does not depend on input array size.
def sort(array, length)
length.times do |index| # outer loop - n elements
min_index = index # assume index is where the next minimally value is
temp_index = index+1 # compare with values at index+1 to length-1
while temp_index < length # inner loop - n-1 elements
if array[temp_index] < array[min_index] # found a new minimum, update min_index
min_index = temp_index
end
temp_index += 1 # move to next index
end
if min_index != index # next minimum value is not at current index, swap
temp = array[min_index]
array[min_index] = array[index]
array[index] = temp
end
end
end
## --- END OF METHODS ---