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
147 lines (133 loc) · 4.29 KB
/
using_restricted_array.rb
File metadata and controls
147 lines (133 loc) · 4.29 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
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: linear O(n). n represents the size/length of the array.
# Space complexity: O(1) -- constant
def length(array)
length = 0
while array[length] != nil
length += 1
end
return length
end
# Prints each integer values in the array
# Time complexity: linear O(n). n is the size/length of array
# Space complexity: O(1) -- constant
def print_array(array)
counter = 0
while array[counter] != nil
print "#{array[counter]} "
counter += 1
end
# raise NotImplementedError
end
# For an unsorted array, searches for 'value_to_find'.
# Returns true if found, false otherwise.
# Time complexity: linear O(n) - n represents length of the array
# Space complexity: O(1)
def search(array, length, value_to_find)
length.times do |i|
return true if array[i] == value_to_find
end
return false
end
# Finds and returns the largest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: O(n) - n represents length of the array
# Space complexity: 0(1)
def find_largest(array, length)
largest_number = array[0]
i = 0
while i < length
if array[i] > largest_number
largest_number = array[i]
end
i += 1
end
return largest_number
end
# Finds and returns the smallest integer value in the array
# Assumes that the array is not sorted.
# Time complexity: O(n) - n represents length of array
# Space complexity: O(1)
def find_smallest(array, length)
smallest_number = array[0]
i = 0
while i < length
if array[i] < smallest_number
smallest_number = array[i]
end
i += 1
end
return smallest_number
end
# Reverses the values in the integer array in place
# Time complexity: O(n) -- still the length of the array
# Space complexity: O(1)
def reverse(array, length)
first = 0
last = length - 1
while first < last
array_reverse = array[last]
array[last] = array[first]
array[first] = array_reverse
first += 1
last -= 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: O (log n) - n is length of array
# Space complexity: O(1)
def binary_search(array, length, value_to_find)
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
low = mid + 1
else
high = mid - 1
end
end
return false
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 ---