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
177 lines (156 loc) · 4.28 KB
/
using_restricted_array.rb
File metadata and controls
177 lines (156 loc) · 4.28 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
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: ?
# Space complexity: ?
#
def length(array)
#raise NotImplementedError
length = 0
while array[length] != nil
length += 1
end
return length
end
# Prints each integer values in the array
# Time complexity: ?
# Space complexity: ?
#
def print_array(array)
# raise NotImplementedError
i = 0
while array[i] != nil
puts array[i]
i += 1
end
end
# For an unsorted array, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: ?
# Space complexity: ?
def search(array, length, value_to_find)
# raise NotImplementedError
i = 0
length.times do
if array[i] == value_to_find
return true
else
i += 1
end
end
return false
end
# Finds and returns the largest integer value the array
# Assumes that the array is not sorted.
# Time complexity: ?
# Space complexity: ?
def find_largest(array, length)
# raise NotImplementedError
i = 0
max_int = 0
return nil if length <= 0
length.times do
if array[i] > max_int
max_int = array[i]
end
i += 1
end
return max_int
end
# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: ?
# Space complexity: ?
def find_smallest(array, length)
# raise NotImplementedError
i = 0
min_int = 221
return nil if length <= 0
length.times do
if array[i] < min_int
min_int = array[i]
end
i += 1
end
return min_int
end
# Reverses the values in the integer array in place
# Time complexity: ?
# Space complexity: ?
def reverse(array, length)
# raise NotImplementedError
if length <= 1
return array
end
i = 0
j = length - 1
hold_arr = RestrictedArray.new(length)
while i < length
hold_arr[i] = array[j]
i += 1
j -= 1
end
i = 0
while i < length
array[i] = hold_arr[i]
i += 1
end
return array
end
# For an array sorted in ascending order, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: ?
# Space complexity: ?
def binary_search(array, length, value_to_find)
# raise NotImplementedError
return false if length <= 0
low = 0
high = length - 1
while low < high
mid = (low + high) / 2
if array[mid] == value_to_find
return true
elsif array[mid] > value_to_find
high = mid - 1
elsif array[mid] < value_to_find
low = mid + 1
end
end
return array[low] == value_to_find
end
# 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 ---