diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..8bf4d45 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,6 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..510e7fc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..198b53d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/restricted-arrays.iml b/.idea/restricted-arrays.iml new file mode 100644 index 0000000..9d4feb8 --- /dev/null +++ b/.idea/restricted-arrays.iml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/using_restricted_array.rb b/lib/using_restricted_array.rb index 90fe0d1..ccd259f 100644 --- a/lib/using_restricted_array.rb +++ b/lib/using_restricted_array.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + 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. @@ -9,45 +11,92 @@ # Time complexity: ? # Space complexity: ? def length(array) - raise NotImplementedError + # array[index] + i = 0 + i += 1 until array[i].nil? + + return i + # raise NotImplementedError end # Prints each integer values in the array # Time complexity: ? # Space complexity: ? def print_array(array) - raise NotImplementedError + # raise NotImplementedError + i = 0 + until array[i].nil? + print 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 -end +def search(array, _length, value_to_find) + # raise NotImplementedError + i = 0 + until array[i] == nil + if array[i] == value_to_find + return true + end + i += 1 + 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 +def find_largest(array, _length) + # raise NotImplementedError + i = 0 + largest = array[i] + until array[i] == nil + if array[i] > largest + largest = array[i] + end + i = i + 1 + end + return largest 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 +def find_smallest(array, _length) + # raise NotImplementedError + i = 0 + smallest = array[i] + until array[i] == nil + if array[i] < smallest + smallest = array[i] + end + i = i + 1 + end + return smallest + end # Reverses the values in the integer array in place # Time complexity: ? # Space complexity: ? def reverse(array, length) - raise NotImplementedError + i = 0 + max = length - 1 + temp = 0 + until i > max + temp = array[i] + array[i] = array[max] + array[max] = temp + i = i + 1 + max = max - 1 + end + return array end # For an array sorted in ascending order, searches for 'value_to_find'. @@ -55,7 +104,13 @@ def reverse(array, length) # Time complexity: ? # Space complexity: ? def binary_search(array, length, value_to_find) - raise NotImplementedError + low = 0 + high = length -1 + + while (low <= high) + # put your code here + + end end # Helper method provided to sort the array in ascending order @@ -75,18 +130,16 @@ def binary_search(array, length, value_to_find) 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 + 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 + min_index = temp_index if array[temp_index] < array[min_index] # found a new minimum, update min_index 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 + next unless 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 OF METHODS ---