Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.DS_Store
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Data Structures
* Arrays
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
* [Sort Squares of an Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
* Binary Trees
* [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb)
* [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
87 changes: 87 additions & 0 deletions data_structures/arrays/sort_squares_of_an_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Arrays - Sorted Squares

# Algorithm challenge description:
# Given an integer array nums sorted in non-decreasing order
# return an array of the squares of each number sorted in non-decreasing order.
# Input: [4, -1, -9, 2]
# Output: [1, 4, 16, 81]

#
# Approach 1: is using Ruby function (for sure)!
#

def sorted_squares(nums)
nums.map! { |num| num**2 }.sort
end
print(sorted_squares([4, -1, -9, 2]))

#
# Approach 2: is using bubble sort
#

def bubble_sort(array)
array_length = array.size
return array if array_length <= 1
loop do
swapped = false
(array_length - 1).times do |i|
if array[i] > array[i + 1]
array[i], array[i + 1] = array[i + 1], array[i]
swapped = true
end
end
break unless swapped
end
array
end

#
# Time complexity: O(n logn), where n is the length of the array.
# Space complexity: O(n) or O(logn)
#

def sorted_squares(nums)
# This takes O(n)
nums.map! { |num| num**2 }
# This can take Ο(n logn)
bubble_sort(nums)
end
print(sorted_squares([4, -1, -9, 2]))

#
# Approach 3: solving without ruby sort method. Using two-pointers
#
# Time complexity: O(n), where n is the length of the array.
# Space complexity: O(n), if you take output into account and O(1) otherwise.
#
def sorted_squares(nums)
p1 = 0
p2 = nums.length - 1
# since we're returing the result in ascending order,
# we'll fill in the array from the end
max_index = p2
output = []
while p1 < p2
nums1_square = nums[p1] * nums[p1]
nums2_square = nums[p2] * nums[p2]
if nums1_square < nums2_square
output[max_index] = nums2_square
p2 -= 1
elsif nums1_square > nums2_square
output[max_index] = nums1_square
p1 += 1
else
output[max_index] = nums1_square
max_index -= 1
output[max_index] = nums2_square
p1 += 1
p2 -= 1
end
max_index -= 1
end
# to account for any remaining value left in the input array
output[max_index] = nums[p1] * nums[p2] if p1 == p2
output
end

print(sorted_squares([4, -1, -9, 2]))